I've found an example code in python that gives out all prime numbers upto n
but I simply don't get it, Why does it does what it does?
I've read the wikipedia article about the Sieve of Eratosthenes but simply have no idea about how this works.
pp = 2
ps = [pp]
lim = raw_input("Generate prime numbers up to what number? : ")
while pp < int(lim):
pp += 1
for a in ps:
if pp%a==0:
break
else:
ps.append(pp)
print set(ps)
An explanation of how the loop works would be appreciated.
EDIT - Figured out that the code's all wrong for it denotes 25 as a prime and through more intensive searching found that this ain't no sieve, can someone show an generator which utilizes the sieves in python and explain it
Firstly, this is not a sieve.
This is how it works.
pp
is the number that we are going to test. In each iteration of the while loop, we go over all the known primes (ps
) and check if they dividepp
. If one of them does,pp
is not a prime, and we move to the next number. Otherwise, we addpp
to the list of primes before moving on.The line
pp%a==0
is basically saying "the remander ofpp
when divided bya
is zero", iea
dividespp
andpp
is not prime.This continues until the number we are checking is larger than some upper limit that we have set (
lim
)[EDIT: this is a sieve]
This is not the most efficient sieve (more efficient ones do things in the
for n in range(2*i, lim, i):
line, but it will work, andisPrime[i]
will be true iffi
is prime.The above implementation produces wrong answers. I've done some changes to the code.
But, here's how the above code works.
We know that the first prime number is 2, so, we generate a list containing only the number
2
.The above line takes an input from the user, which gives us the upper limit of the prime numbers to generate.
The numbered lines do the following things.
pp
variable by 1.for
loop iterates over the list of prime numbers stored inps
and checks that the current number,pp
is divisible by any one of those numbers, if yes, then the number is not prime and theprimeFlag
is set toFalse
and we break out of the innerfor
loop.primeFlag
isTrue
and theif
statement appends the listps
withpp
.Since no one has yet to show a true sieve or explain it, I will try.
The basic method is to start counting at 2 and eliminate 2*2 and all higher multiples of 2 (ie 4, 6, 8...) since none of them can be prime. 3 survived the first round so it is prime and now we eliminate 3*3 and all higher multiples of 3 (ie 9, 12, 15...). 4 was eliminated, 5 survived etc. The squaring of each prime is an optimization that makes use of the fact that all smaller multiples of each new prime will have been eliminated in previous rounds. Only the prime numbers will be left as you count and eliminate non-primes using this process.
Here is a very simple version, notice it does not use modulo division or roots:
The simple approach above is surprisingly fast but does not make use of the fact that primes can be only odd numbers.
Here is a generator based version that is faster than any other I have found but hits a Python memory limit at n = 10**8 on my machine.
Here is a slightly slower but much more memory efficient generator version:
To test if a number is prime just do:
Here are some hints as to how this more memory efficient version works. It uses a
dict
to store only the bare minimum of information, the next non-prime numbers (as keys) along with their factors (as values). As each non-prime is found in thedict
, it is removed and the next non-prime key is added with the same factor value.That code is an attempt at using trial division to produce a sequence of primes.
To correct it:
To make it much more efficient (in fact, optimal) trial division: