python 素数最快,一种快速素数筛在Python

I have been going through prime number generation in python using the sieve of Eratosthenes and the solutions which people tout as a relatively fast option such as those in a few of the answers to a question on optimising prime number generation in python are not straightforward and the simple implementation which I have here rivals them in efficiency. My implementation is given below

def sieve_for_primes_to(n):

size = n//2

sieve = [1]*size

limit = int(n**0.5)

for i in range(1,limit):

if sieve[i]:

val = 2*i+1

tmp = ((size-1) - i)//val

sieve[i+val::val] = [0]*tmp

return sieve

print [2] + [i*2+1 for i, v in enumerate(sieve_for_primes_to(10000000)) if v and i>0]

Timing the execution returns

python -m timeit -n10 -s "import euler" "euler.sieve_for_primes_to(1000000)"

10 loops, best of 3: 19.5 msec per loop

While the method described in the answer to the above linked question as being the fastest from the python cookbook is given below

import itertools

def erat2( ):

D = { }

yield 2

for q in itertools.islice(itertools.count(3), 0, None, 2):

p = D.pop(q, None)

if p is None:

D[q*q] = q

yield q

else:

x = p + q

while x in D or not (x&1):

x += p

D[x] = p

def get_primes_erat(n):

return list(itertools.takewhile(lambda p: p

When run it gives

python -m timeit -n10 -s "import euler" "euler.get_primes_erat(1000000)"

10 loops, best of 3: 697 msec per loop

My question is why do people tout the above from the cook book which is relatively complex as the ideal prime generator?

解决方案

I transformed your code to fit into the prime sieve comparison script of @unutbu at Fastest way to list all primes below N in python

as follows:

def sieve_for_primes_to(n):

size = n//2

sieve = [1]*size

limit = int(n**0.5)

for i in range(1,limit):

if sieve[i]:

val = 2*i+1

tmp = ((size-1) - i)//val

sieve[i+val::val] = [0]*tmp

return [2] + [i*2+1 for i, v in enumerate(sieve) if v and i>0]

On my MBPro i7 the script is fast calculating all primes < 1000000 but actually 1.5 times slower than rwh_primes2, rwh_primes1 (1.2), rwh_primes (1.19) and primeSieveSeq (1.12) (@andreasbriese at the page end).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值