下一个应该是 33550336。
您的代码(我修复了缩进,因此原则上它可以满足您的需求):
i = 2
a = open("perfect.txt", 'w')
a.close()
while True:
sum = 0
for x in range(1, i+1):
if i%x == 0:
sum += x
if sum / 2 == i:
a = open("perfect.txt", 'a')
a.write(str(i) + "\n")
a.close()
i += 1
对i 进行除数以找到i 的除数。
所以要找到直到n 的完美数字,它确实可以
2 + 3 + 4 + ... + (n-1) + n = n*(n+1)/2 - 1
for 循环中的分区。
现在,对于n = 33550336,那就是
Prelude> 33550336 * (33550336 + 1) `quot` 2 - 1
562812539631615
大约 5.6 * 1014 个分区。
假设您的 CPU 每秒可以进行 109 分区(很可能不能,根据我的经验,108 是一个更好的估计,但即便如此机器 ints 在 C 中),这将需要大约 560,000 秒。一天有 86400 秒,因此大约是六天半(按 108 估计超过两个月)。
您的算法太慢了,无法在合理的时间内达到。
如果你不想使用数论(即使是完美的数字也有一个非常简单的结构,如果有奇完美的数字,它们一定是巨大的),你仍然可以通过只除以做得更好求除数的平方根,
i = 2
a = open("perfect.txt", 'w')
a.close()
while True:
sum = 1
root = int(i**0.5)
for x in range(2, root+1):
if i%x == 0:
sum += x + i/x
if i == root*root:
sum -= x # if i is a square, we have counted the square root twice
if sum == i:
a = open("perfect.txt", 'a')
a.write(str(i) + "\n")
a.close()
i += 1
只需要大约 1.3 * 1011 个除法,并且应该在几个小时内找到第五个完美数字。
不求助于偶完美数的显式公式(2^(p-1) * (2^p - 1) 用于素数 p 使得 2^p - 1 是素数),您可以通过找到 i 的素数分解并计算除数来加快速度总和。这将使所有合数的测试更快,并且对大多数人来说更快,
def factorisation(n):
facts = []
multiplicity = 0
while n%2 == 0:
multiplicity += 1
n = n // 2
if multiplicity > 0:
facts.append((2,multiplicity))
d = 3
while d*d <= n:
if n % d == 0:
multiplicity = 0
while n % d == 0:
multiplicity += 1
n = n // d
facts.append((d,multiplicity))
d += 2
if n > 1:
facts.append((n,1))
return facts
def divisorSum(n):
f = factorisation(n)
sum = 1
for (p,e) in f:
sum *= (p**(e+1) - 1)/(p-1)
return sum
def isPerfect(n):
return divisorSum(n) == 2*n
i = 2
count = 0
out = 10000
while count < 5:
if isPerfect(i):
print i
count += 1
if i == out:
print "At",i
out *= 5
i += 1
在我的机器上估计需要 40 分钟。
不错的估计:
$ time python fastperf.py
6
28
496
8128
33550336
real 36m4.595s
user 36m2.001s
sys 0m0.453s