我想解决项目欧拉中的10个问题。它包括找到所有素数低于200万的总和。我根据Eratosthenes的Sieve编写了以下代码。如何使Eratosthenes的筛子更快?
import time
t0 = time.time()
n=200000
liste=list(range(2,n))
k=2
s=2
while k <=n:
liste=list(set(liste)-set(range(k,n,k)))
if liste!=[]:
k=min(liste)
s+=k
else:
break
print(s)
t1 = time.time()
total = t1-t0
print(total)
我测试了上面的代码n = 200000,但它对于n = 2000000太慢了。我非常感谢能够帮助改善这个计划。
http://stackoverflow.com/a/23423821/2141635使用总和,你有答案 –
也有关:http://stackoverflow.com/q/2068372/1639625 –
相关:[加快位串/位操作在Python中?](http://stackoverflow.com/q/2897297/4279) – jfs