<p>以下是一些可以让主生成器更高效的方法:</p>
<pre><code>def primesieve(limit):
primelist=[]
# Don't create a list of all your numbers up front.
# And even if you do, at least skip the even numbers!
#for i in xrange(limit):
# primelist.append(i)
# Skip counting - no even number > 3 is prime!
for i in xrange(3, limit, 2):
# You only need to check up to the square root of a number:
# I *thought* that there was some rule that stated that a number
# was prime if it was not divisible by all primes less than it,
# but I couldn't find that for certain. That would make this go
# a lot faster if you only had to check primes and numbers greater
# than the greatest prime found so far up to the square root of
# the number
for divisor in xrange(3, int(i**0.5)+1, 2):
if not i % divisor: # no remainder, so sad
break
else:
# loop exited naturally, number has no divisors hooray!
primelist.append(i)
# Need to put the number 2 back, though
primelist.insert(0, 2)
return primelist
</code></pre>
<p>这使用了<em>混乱</em>我的CPU(100%或更多,万岁!)但几乎不使用任何内存(比如,一个7分钟内存的几MB内存)。我的CPU只有2.5GHz左右,到目前为止,<code>10**8</code>作为最大主处理器已经用了7分钟。你知道吗</p>
<p>如果你看我在评论中链接的帖子,有一些更好的方法来生成素数,但是这些是一些简单的改进。你知道吗</p>