/ Published in: Python
                    
                                        
Function (algorithm) that returns all prime numbers  up to n.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
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
Comments
 Subscribe to comments
                    Subscribe to comments
                
                