The Sieve of Eratosthenes, Explained

After reading this you can run the oldest prime-finding algorithm by hand, predict exactly which cells survive, count the crossing-out work it costs, and read the diagonal stripes the multiples make in a grid.

What the sieve does

The Sieve of Eratosthenes lists every prime up to some limit N by removing composites instead of testing numbers one at a time. Write the integers from 2 to N. Take the smallest number not yet crossed out, call it prime, then cross out all of its multiples. Repeat. The numbers that survive are exactly the primes.

Here is the hook. To find every prime up to 30 you only ever cross out multiples of 2, 3, and 5. That is three passes. After you strike out multiples of 5, everything left standing (7, 11, 13, 17, 19, 23, 29 and the earlier survivors) is prime, and you never had to divide anything. The sieve replaces division with the far cheaper act of counting up in steps.

Why does it work? Every composite number has a prime factor, so when you reach the smallest such factor you cross the composite out as one of its multiples. Nothing composite can slip through. Nothing prime is ever crossed, because a prime has no smaller factor to cross it. The survivors are precisely the primes.

When to use it, and when not to

The sieve shines when you want all primes up to a fixed limit. For N in the millions it is fast and simple, and it needs only one bit of memory per number. That makes it the standard first step whenever a program needs a table of small primes.

It is the wrong tool for a single large number. To test whether one specific 200-digit number is prime, you would never build a table with 10^{200} entries. That job belongs to probabilistic tests like Miller-Rabin. The sieve is a bulk producer, not a spot-checker.

The memory cost is one bit per candidate, so a plain sieve to N = 10^8 needs about 12.5 megabytes. Past a few billion you switch to a segmented sieve that processes one window of the number line at a time.

The stopping rule and the count of work

You can stop early. Once the current prime p satisfies p^2 \gt N, every remaining uncrossed number is already prime. The reason is short: any composite c \le N has a prime factor no larger than \sqrt{N}, so it was crossed on some earlier pass.

\text{stop after the last prime } p \text{ with } p \le \sqrt{N}

Here p is the prime you are currently processing and N is the top of your list. For N = 30 you have \sqrt{30} \approx 5.48, so you stop after crossing multiples of 5. The primes 7 through 29 fall out for free.

The total crossing-out work is the sum, over each prime p, of how many multiples of p lie in range, which is about N/p each. Summing gives a clean estimate.

\sum_{p \le N} \frac{N}{p} \approx N \ln \ln N

The left side counts the strikes: N/p multiples for each prime p. The right side uses the fact that the sum of 1/p over primes up to N grows like \ln \ln N. That double logarithm grows painfully slowly. For N = 10^6, \ln \ln N \approx 2.63, so the sieve does roughly 2.6 million strikes to find all 78498 primes below a million.

A worked run to 30

Sieving the numbers 2 to 30

The demo starts with the default limit and a grid. Follow the passes and cross out by hand.

  1. The smallest uncrossed number is 2. It is prime. Cross out 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30. That is 14 strikes.
  2. The next uncrossed number is 3. Prime. Cross out 6, 9, 12, 15, 18, 21, 24, 27, 30. Some (6, 12, 18, 24, 30) were already gone; you still visit 9 strikes, 4 of them new.
  3. The next uncrossed number is 5. Prime. Cross out 10, 15, 20, 25, 30. New crossings: 25.
  4. The next uncrossed number is 7. Check the stopping rule: 7^2 = 49 \gt 30. Stop crossing.

Everything still standing is prime: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. That is 10 primes below 30. You can confirm the count matches the exact list.

A grid of the numbers 2 to 120 with multiples of 2, 3, 5 and 7 crossed out in four colours. Changing the row width slides the crossed cells onto different diagonal lines: with width 6 the multiples of 2 and 3 collapse into just two columns, exposing why primes above 3 all sit in the columns for 6k+1 and 6k+5.

Reading the diagonal stripes

Lay the numbers in a grid of fixed width w and the multiples of a prime p form a lattice. A multiple of p sits p steps to the right of the previous one. In a grid of width w, stepping right by p means moving p \bmod w columns over and wrapping down. When w is a multiple of p, the step lands in the same column each time, so you see straight vertical bands. Otherwise the marks slant into diagonals.

Width 6 is the instructive case. Every multiple of 2 lands in columns 2, 4, 6, and every multiple of 3 in columns 3 and 6. That leaves only columns 1 and 5 clean, which is the statement that every prime past 3 has the form 6k \pm 1. The grid makes an algebraic fact visible.

The first prime does most of the work; each later prime strikes fewer new cells because many multiples are already gone.

How the work thins out

The bar chart above shows a pattern that holds at every scale: the small primes carry the load. Multiples of 2 alone remove half the numbers. Multiples of 3 among the rest remove a third of those, and so on. The count of survivors after sieving with the first few primes follows the product below.

S = N \prod_{p \le B} \left(1 - \frac{1}{p}\right)

Here S estimates how many numbers survive after crossing multiples of every prime p up to a bound B, starting from N candidates. For N = 30 sieved by 2, 3, 5 you get 30 \cdot \frac{1}{2} \cdot \frac{2}{3} \cdot \frac{4}{5} = 8, close to the 10 primes actually found (the estimate ignores that 2, 3, 5 themselves survive). This product is the seed of Mertens' theorem and the prime number theorem's density estimate.

Each new prime removes a smaller slice. The survival fraction keeps dropping but the drops shrink, matching the slow growth of the crossing count.

Common mistakes

Three errors trip people up when they code or hand-run the sieve.

Starting the strikes at the wrong place
When processing prime p, begin crossing at p^2, not at 2p. Every smaller multiple of p, such as 2p or 3p, already carries a smaller prime factor and was struck earlier. Starting at p^2 avoids repeat work.
Testing primality by division inside the sieve
The whole point is to avoid division. You never ask "is 91 prime?" You simply notice 91 was crossed when you processed 7. If a cell is still standing when you reach it, it is prime by construction.
Ignoring the stopping rule
Crossing multiples of primes above \sqrt{N} wastes time and finds nothing new. For a sieve to a million, you stop after 997, the largest prime below 1000, even though there are 78498 primes in total.

Do not confuse the sieve's output with a primality test for one number. The sieve is efficient only because it shares work across the whole range. Building the table to check a single number is the slowest possible method.

Related tools

Once you have the primes, the patterns are worth chasing. The Ulam Prime Spiral takes the same integers, coils them outward and marks the primes, and the diagonal streaks that appear echo the grid stripes here. The Goldbach Comet asks how each even number splits into two of your primes. For the machinery behind divisibility, the Euclidean Algorithm Visualizer shows greatest common divisors as tiled squares, and Modular Times Table and Pascal's Triangle mod n both turn remainders into pictures. If the grid-of-cells idea appeals, Conway's Game of Life and Elementary Cellular Automata run rules across grids of their own.

Frequently asked questions

Why is it called a sieve?

A sieve keeps what you want and lets the rest fall through. Here the composites are shaken out as multiples, and the primes are the grains too big to pass. Eratosthenes of Cyrene described it around 240 BCE.

How fast is the Sieve of Eratosthenes?

Its running time is about N \ln \ln N operations, close to linear in N. To a million that is roughly 2.6 million strikes. The double logarithm grows so slowly that doubling N barely changes the cost per number.

Should I start crossing at 2p or at p squared?

Start at p^2. Every multiple kp with k \lt p was already crossed when you processed the prime factors of k, so those strikes are redundant.

What is a segmented sieve?

It sieves one window of the number line at a time using the primes up to \sqrt{N}, so you never hold the whole array in memory. That is how you sieve to billions on an ordinary machine.

Why do all primes above 3 have the form 6k plus or minus 1?

Any integer is one of 6k, 6k{+}1, 6k{+}2, 6k{+}3, 6k{+}4, 6k{+}5. The forms 6k, 6k{+}2, 6k{+}4 are even and 6k{+}3 is divisible by 3, so only 6k{+}1 and 6k{+}5 can be prime past 3. The width-6 grid shows this as two clean columns.