Skip to content
It's Algorithm PowerCatalogMy algorithmsNew

Sieve of Eratosthenes

algorithmseratosthenesmathprimes
Version 1 · PythonVersion 2 · PythonVersion 3 · Python

Returns primes < n.


see :
- here and here
- Recipe 117119

Python
1 2 3 4 5 6 7 8
def eratosthenes(n):
    N=range(n+1)
    z=[0]*(n/2)
    for i in range(2, int(n**.5)+1):
        if N[i]:
            N[i*i::i] = z[:(n/i)-i+1]
    return filter(None, N[2:])

code.activestate.com ↗

Algorithm PowerBack to top ↑