我会说,“改为使用xrange()”,但实际上您使用的是int列表作为筛选结果。。。。。所以整数生成器不是正确的解决方案。在
我认为,无论您使用什么函数来实现一个包含3931232332323123个元素的列表都是困难的。。。。也就是说,毕竟是279 PB的64位整数。在
试试这个。在class FoundComposite(Exception): pass
primes = [2]
seq = itertools.takewhile( # Take integers from a list
lambda x: x
itertools.count(2) # the list of integers starting from 2
)
#seq = xrange(2, MAXNUM) # alternatively
for i in seq:
try:
for divisor in primes:
if not (i % divisor):
# no remainder - thus an even divisor
# continue to next i in seq
raise FoundComposite
# if this is reached, we have tried all divisors.
primes.append(i)
except FoundComposite:
pass