Sieve of Eratosthenes - Finding Primes Python Sieve of Eratosthenes - Finding Primes Python python python

Sieve of Eratosthenes - Finding Primes Python


You're not quite implementing the correct algorithm:

In your first example, primes_sieve doesn't maintain a list of primality flags to strike/unset (as in the algorithm), but instead resizes a list of integers continuously, which is very expensive: removing an item from a list requires shifting all subsequent items down by one.

In the second example, primes_sieve1 maintains a dictionary of primality flags, which is a step in the right direction, but it iterates over the dictionary in undefined order, and redundantly strikes out factors of factors (instead of only factors of primes, as in the algorithm). You could fix this by sorting the keys, and skipping non-primes (which already makes it an order of magnitude faster), but it's still much more efficient to just use a list directly.

The correct algorithm (with a list instead of a dictionary) looks something like:

def primes_sieve2(limit):    a = [True] * limit                          # Initialize the primality list    a[0] = a[1] = False    for (i, isprime) in enumerate(a):        if isprime:            yield i            for n in range(i*i, limit, i):     # Mark factors non-prime                a[n] = False

(Note that this also includes the algorithmic optimization of starting the non-prime marking at the prime's square (i*i) instead of its double.)


def eratosthenes(n):    multiples = []    for i in range(2, n+1):        if i not in multiples:            print (i)            for j in range(i*i, n+1, i):                multiples.append(j)eratosthenes(100)


Removing from the beginning of an array (list) requires moving all of the items after it down. That means that removing every element from a list in this way starting from the front is an O(n^2) operation.

You can do this much more efficiently with sets:

def primes_sieve(limit):    limitn = limit+1    not_prime = set()    primes = []    for i in range(2, limitn):        if i in not_prime:            continue        for f in range(i*2, limitn, i):            not_prime.add(f)        primes.append(i)    return primesprint primes_sieve(1000000)

... or alternatively, avoid having to rearrange the list:

def primes_sieve(limit):    limitn = limit+1    not_prime = [False] * limitn    primes = []    for i in range(2, limitn):        if not_prime[i]:            continue        for f in xrange(i*2, limitn, i):            not_prime[f] = True        primes.append(i)    return primes