# Prime Sieve of Eratosthenes

The Sieve of Eratosthenes is an algorithm for generating a list of all prime numbers up to a given integer N. It does this using ${\displaystyle O(N)}$ space and ${\displaystyle O\left({\frac {N\log \log N}{\log N}}\right)}$ time. (Note that ${\displaystyle N}$ is the number. The size of the input would be the number of bits representing the number, which can be stored in ${\displaystyle \log N}$ bits for ${\displaystyle N}$.)

## Algorithm

The Prime Sieve of Eratosthenes algorithm precalculates from the bottom up - given a table of ${\displaystyle N}$ values, we "knock off" numbers that can't possibly be a prime - by counting up from hasn't been knocked off.

### Example

Let ${\displaystyle N=30}$:

 1  2  3  4  5  6  7  8  9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30


We know that 1 isn't a prime, so it's safe to cross it off. So we'll start at 2:

    2  3  4  5  6  7  8  9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30


The smallest number that hasn't been processed is two, now let's knock out multiples of twos - by first going to ${\displaystyle 4,6,8\ldots }$ - all you need is addition!

   *2  3     5     7     9
11    13    15    17    19
21    23    25    27    29


Now that two is done, we can cross out multiples of threes - 6 is already crossed out, but 9 isn't, so we're going to cross that out, continuing..

   *2 *3     5     7
11    13          17    19
23    25          29


And continuing on.

### Pseudocode

In its simplest form, without optimizations:

func sieve( var N )
var PrimeArray as array of size N
initialize PrimeArray to all true

for i from 2 to N
for each j from i + i to N, step i
set PrimeArray( j ) = false


At the end, ${\displaystyle PrimeArray(x)}$ will be true if ${\displaystyle x>1}$ is a prime number.

This is not the sieve of Eratosthenes though, because that algorithm sieves only by primes, as they are discovered, while this algorithm sieves by all natural numbers above 1. It is very easily parallelizable though.

### Optimizations

1. If ${\displaystyle m}$ is a multiple of ${\displaystyle i}$ and ${\displaystyle i}$ is a multiple of ${\displaystyle p}$, then ${\displaystyle m}$ is also a multiple of ${\displaystyle p}$. Thus, if ${\displaystyle i}$ has already been crossed out when the algorithm reaches it, we need not go crossing out its multiples, because they would have already been crossed out.
2. If a number ${\displaystyle m}$ is not prime, then it has a factor that is ${\displaystyle \leq {\sqrt {m}}}$. (This is because if ${\displaystyle m=pq}$, then at least one of ${\displaystyle p}$ and ${\displaystyle q}$ must be ${\displaystyle \leq {\sqrt {m}}}$.) Thus, any number that is ever crossed out will be crossed out first by a number less than or equal to its square root. Putting this another way, we need not cross out multiples ${\displaystyle m}$ of ${\displaystyle i}$ that are less than ${\displaystyle i^{2}}$, because if ${\displaystyle m is composite, it would have already been crossed out by some number smaller than ${\displaystyle i}$.
3. By the above, as we will do nothing for ${\displaystyle i}$ such that ${\displaystyle i^{2}}$ exceeds ${\displaystyle N}$, we can stop when that happens.

With these observations, we can write the following algorithm:

Start with an array is_prime[${\displaystyle 2..N}$], all initialized to true.
For each ${\displaystyle i}$ from ${\displaystyle 2}$ to ${\displaystyle N}$, do
If isprime[${\displaystyle i}$] is true,
For each ${\displaystyle m}$ from ${\displaystyle i^{2}}$ to ${\displaystyle N}$, step ${\displaystyle i}$
set is_prime[${\displaystyle m}$] to false  ("cross it out")


This algorithm is usually what is meant when "the sieve of Eratosthenes" is mentioned.

#### Other optimizations

• There is no need to store even numbers, as there is only one even prime number. (2, of course.)
• You can use bitmaps to pack the memory.
• If you are looking for only prime numbers you can generate them and keep adding them to a list.