Eratosthenes筛
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eratosthenes筛相关的知识,希望对你有一定的参考价值。
Function (algorithm) that returns all prime numbers up to n.
def E_sieve(n): A=[] for p in range(2, n+1): # fils the list with numbers from 2 up to n A.append(p) for p in range(2, int(floor(n)-1)): if A[p]!=0: # cheks if p had been eliminated in any of the previous passess j=p*p while j<=n-2: A[j]=0 # marks element as eliminated j+=p # copying the remaining elements from list A into list L i=0 L=[] for p in range(2, n-1): if A[p]!=0: L.append(p) i+=1 return L
以上是关于Eratosthenes筛的主要内容,如果未能解决你的问题,请参考以下文章
简单质数筛法-试除法,Eratosthenes筛法,线性筛法