如果您了解 numpy 以及 Python,请查看 primesfrom2to 的这个实现,取自 this answer in StackOverflow。
def primesfrom2to(n):
# https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
""" Input n>=6, Returns a array of primes, 2 <= p < n """
sieve = np.ones(n/3 + (n%6==2), dtype=np.bool)
sieve[0] = False
for i in xrange(int(n**0.5)/3+1):
if sieve[i]:
k=3*i+1|1
sieve[ ((k*k)/3) ::2*k] = False
sieve[(k*k+4*k-2*k*(i&1))/3::2*k] = False
return np.r_[2,3,((3*np.nonzero(sieve)[0]+1)|1)]
在我链接到的那个答案中,这个例程在构建素数列表方面是最快的。我在自己的代码中使用了它的一个变体来探索素数。详细解释这一点需要很多篇幅,但它构建了一个 sieve,它忽略了 2 和 3 的倍数。只有两行代码(以 sieve[ 和以 = False 结尾的代码)标记了新的倍数-找到素数。我认为这就是您所说的“跳跃……在获得最佳状态后”。代码很棘手,但正在学习中。该代码适用于 Python 2,旧版 Python。
这是我自己的一些具有更多 cmets 的 Python 3 代码。可以拿来做对比。
def primesieve3rd(n):
"""Return 'a third of a prime sieve' for values less than n that
are in the form 6k-1 or 6k+1.
RETURN: A numpy boolean array. A True value means the associated
number is prime; False means 1 or composite.
NOTES: 1. If j is in that form then the index for its primality
test is j // 3.
2. The numbers 2 and 3 are not handled in the sieve.
3. This code is based on primesfrom2to in
<https://stackoverflow.com/questions/2068372/
fastest-way-to-list-all-primes-below-n-in-python/
3035188#3035188>
"""
if n <= 1: # values returning an empty array
return np.ones(0, dtype=np.bool_)
sieve = np.ones(n // 3 + (n % 6 == 2), dtype=np.bool_) # all True
sieve[0] = False # note 1 is not prime
for i in range(1, (math.ceil(math.sqrt(n)) + 1) // 3): # sometimes large
if sieve[i]:
k = 3 * i + 1 | 1 # the associated number for this cell
# Mark some of the stored multiples of the number as composite
sieve[k * k // 3 :: 2 * k] = False
# Mark the remaining stored multiples (k times next possible prime)
sieve[k * (k + 4 - 2*(i&1)) // 3 :: 2 * k] = False
return sieve
def primesfrom2to(n, sieve=None):
"""Return an array of prime numbers less than n.
RETURN: A numpty int64 (indices type) array.
NOTES: 1. This code is based on primesfrom2to in
<https://stackoverflow.com/questions/2068372/
fastest-way-to-list-all-primes-below-n-in-python/
3035188#3035188>
"""
if n <= 5:
return np.array([2, 3], dtype=np.intp)[:max(n - 2, 0)]
if sieve is None:
sieve = primesieve3rd(n)
elif n >= 3 * len(sieve) + 1 | 1: # the next number to note in the sieve
raise ValueError('Number out of range of the sieve in '
'primesfrom2to')
return np.r_[2, 3, 3 * np.nonzero(sieve)[0] + 1 | 1]
这里有什么不懂的可以问我。