Sieve of Eratosthenes





-1
Date Submitted Sun. Oct. 31st, 2010 4:13 PM
Revision 1 of 1
Helper Doctor_Who
Tags "eratosthenes" | "python" | "sieve"
Comments 0 comments
An algorithm that returns prime numbers from 2 up to

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
 

Doctor Who

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down


Helper Doctor_Who