我在
python上做了完美数字的实验室它运行良好并打印了我需要的数字.但不确定我是否需要将(1,1000)放入范围或(2,n 1)是否正常?我的指示要求我
“编写一个python程序,找到从1到10,000的所有完美数字.当找到一个完美的数字时,你的逻辑应该打印它.”
什么是完全数:
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.
当我运行我的程序时,它打印出6,28,496和8128.
n = 1
while True:
factors = [1]
[factors.append(i) for i in range(2,n+1) if n%i == 0]
if sum(factors) == 2*n: print n
n += 1