def primes(n):
if n==2: return [2]
elif n<2: return []
s=range(3,n+1,2)
mroot = n ** 0.5
half=(n+1)/2-1
i=0
m=3
while m <= mroot:
if s[i]:
j=(m*m-3)/2
s[j]=0
while j<half:
s[j]=0
j+=m
i=i+1
m=2*i+3
return [2]+[x for x in s if x]
Hello, I found this function here http://code.activestate.com/recipes/366178-a-fast-prime-number-list-generator/ and I’m stuck. I don’t understand this. I think it’s using some properties of prime numbers but all those single letter variables are so mysterious. Could you please shed some light?
This I understand:
mroot is the limit of the numbers you want to check for primality
and I know that the function changes the list s to 0’s marking the multiples. I also understand the list comprehesion at the end, and I understand s.
But why half? What is j? What is m?
Could you please give some comment?
A line-by-line breakdown:
This function returns a list of primes
<= n. So ifn == 2, that list contains only 2. Easy enough.Here again, there are no prime numbers below 2, so return an empty list.
So
sis a list of odd numbers starting with 3 and going ton + 1.sstands for sieve — this is a sieve algorithm, which means that composite (non-prime) numbers will be sifted out of the list. In effect, they will be “crossed out.” See below for a detailed description of sieve algorithms.Since it’s a sieve, we can stop the algorithm once we’ve hit the square root of
n.This is an explicit formula for the length of s; it could be replaced with
len(s), but that might take longer to calculate for large values of n. This will also be useful for terminating certain parts of the algorithm.I is our index; i simply steps through the sieve, checking each value. If the value is
0, then the number has been “crossed off” because it isn’t prime.mis simply the value ofs[i]at any given moment; a later line keeps it updated.Since
s[i]evaluates toTrue, it hasn’t been crossed off the list yet. That means it’s prime! So now we have to figure out which numbers on the list are multiples ofs[i]— they are all non-primes, and should be crossed off the list.Now the fun starts. Because the sieve isn’t a list of consecutive numbers, but a list of odd numbers, we have to figure out where the multiples of our prime live in
s. In this case, our prime is3, so we need to find the index of 9, 15, 21, 27… (we don’t have to find 6, 12, 18… because they’re even, and so not in the list). This particular technique for finding the indices is really clever, because the author has figured out that once all the multiples of a particular prime have been crossed out, they can be skipped. That means the first un-crossed-out multiple of our prime is actually the square of that prime. (So for example, if the prime were 7, 7 * 3 = 21 and 7 * 5 = 35 would already have been crossed out, so the first multiple of 7 that we have to deal with is 7 * 7.) Once that makes sense, it’s pretty easy to see that the location of 9 insis (9 – 3) // 2 (where // is floor division).Now it goes back to being easy. We’ve found 9; now we have to find 15 = 9 + 3 + 3. Since
scontains only odd numbers, it’s half as long as a list with every number; to skip ahead 6 then, we need only add 3 toj. We do this as long asjis less thanhalf— in other words, as long asjis less than the length ofs.Again, easy —
iis just the index of the list, whilemis the value of the number that was originally there. (You can test it out to see why:[2 * i + 3 for i in range(10)].)And voila — filter the zeros out of the sieve, prepend [2] and you have a list of primes.
The most confusing thing about this algorithm has to do with the shortcuts the author has taken, which make this run faster, but fog up the fundamental concept. (In fact, there are even more shortcuts one could take, but that’s another post.) Here’s a much simpler sieve that shows the basic idea:
To spell it all out, first numbers looks like this:
Then…
And so on.