How do you actually find every prime up to some limit? You could test each number one at a time — try dividing $91$ by $2, 3, 5, 7\dots$ — but that's slow, and you throw the work away after every single number. Eratosthenes, the chief librarian of the great Library of Alexandria (the same man who, without ever leaving Egypt, worked out the circumference of the whole Earth from two shadows), found a cleverer way around 200 BC. It needs no division at all — just systematic, mechanical crossing-out — and it is still, over two thousand years later, the fastest simple method known for listing primes.
The trick is to stop hunting for primes one by one, and instead find them all at once by sieving the composites away — striking out every number that isn't prime until only primes remain standing.
Eratosthenes was such an all-round scholar — geography, poetry, astronomy, athletics, maths — that his rivals in Alexandria nicknamed him "Beta," second-best at everything, never quite first at any one thing. History has been kinder: his circumference of the Earth was startlingly close to the true value, and his prime-finding trick is the one idea of his that every student still meets today, unchanged, twenty-two centuries on.
Whatever is never struck is prime. There is one beautiful shortcut worth knowing:
you can stop once you pass
Let's do the whole thing on paper. Write out the numbers from $2$ to $30$, and work through them exactly as the rule says.
Notice how little work that was: three primes ($2$, $3$, $5$) did all the striking, and together they cleared out every composite up to $30$ before we ever had to think about $7, 11$, or anything bigger.
The shortcut gets more valuable the bigger $N$ gets. Suppose you want every prime up to $100$.
Since
Four primes — $2, 3, 5, 7$ — are enough to sift out every composite among the $99$ numbers from $2$ to $100$, leaving the $25$ primes below $100$ standing. Compare that to testing all $99$ numbers individually by trial division: the sieve does dramatically less work, because each prime strikes its own multiples exactly once instead of every number being checked against every possible divisor.
Counting the actual strikes makes the saving concrete. For $N = 100$:
Roughly $80$ strikes in total lay bare all $99$ numbers, and every single strike is a plain addition (jump to the next multiple) — never a division. Trial-dividing every one of the $99$ numbers by every smaller candidate divisor would take vastly more individual checks for the same answer.
Every composite number has a smallest prime factor
Take $n = 91$. Its smallest prime factor is $7$, since $91 = 7 \times 13$. Sure enough, when the sieve reaches $p = 7$ and starts striking $14, 21, 28, \dots$, it eventually lands on $91 = 7 \times 13$ and crosses it out right there — even though $91$ survived the earlier passes of $2, 3$, and $5$ completely untouched, because none of them divide it. Every composite has exactly one first moment where its turn comes; the sieve never misses it, and never wastes a strike on a number that's already gone.
Almost everyone trips over one of these the first time they run the sieve:
Remarkably, yes — and not just as a curiosity. Modern cryptography (the maths that keeps your
banking and messaging apps secure) needs enormous prime numbers, hundreds of digits long, and
finding them means testing huge random candidates for primality. A full
Only the survivors of that quick, cheap sieve — Eratosthenes' exact idea, run on a silicon chip instead of a wax tablet — go on to the slow, expensive test. In fact the saving is huge: sieving candidates against the first few thousand small primes throws away the overwhelming majority of random numbers almost instantly, so the expensive test only ever runs on the small fraction that might actually be prime. A librarian's trick from ancient Alexandria is, quite literally, running inside your phone right now, every time it sets up a secure connection.