列表

详情


NC25046. [USACO 2007 Jan L]Factors

描述

Integers greater than 1 have more than one factor: a factor is a number that divides evenly into an integer. The number 6 has factors of 1, 2, 3, and 6. Ignoring the number 6 itself, it's interesting to note that 1+2+3=6, which makes 6 a "perfect number". The number 14 has factors 1, 2, 7, and 14. Their sum (ignoring 14 itself) is 10.
In the ongoing quest for perfect numbers, write a program that reads a single integer (1 <= N <= 10,000,000) and prints the sum of its factors (not including the number itself).

输入描述

Line 1: A single integer, N

输出描述

Line 1: A single integer that is the sum of the factors of N (not including N itself)

示例1

输入:

314

输出:

160

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 42ms, 内存消耗: 4572K, 提交时间: 2023-08-16 09:21:41

N = int(input())

# 分解质因数
def getFactors(n):
    r = {1}
    for k in range(2, int(n ** 0.5) + 1):
        if n % k == 0:
            r.add(k)
            r.add(n // k)
    return r
    
s = sum(getFactors(N))
print(s)

上一题