Crossing out is cheaper than testing
Testing each number separately repeats work. The sieve marks each composite once per distinct prime factor and no more.
The oldest algorithm still in use. It finds every prime up to n by removing multiples, stops at √n for the reason “Primes and Unique Factorisation” gave, and costs far less than testing each number separately.
Core ideas
The sieve is the standard example of an algorithm that beats the obvious approach by reorganising the work.
Testing each number separately repeats work. The sieve marks each composite once per distinct prime factor and no more.
Multiples of p below p² have a smaller prime factor and were already crossed out. Starting at 2p is correct and wasteful.
After sieving by every prime up to √n, anything uncrossed has no factor below its square root and is prime, by “Primes and Unique Factorisation”'s bound.
The material
The algorithm, its two optimisations, and its cost.
Two things make it efficient. The outer loop stops at √n because anything surviving that far has no prime factor below its own square root. The inner loop starts at p² because a multiple kp with k < p has the prime factor k or a factor of it, and was removed earlier.
After the sieve, an integer m in 2, …, n is uncrossed if and only if it is prime.
A prime is never crossed out: it is only removed as a multiple kp with k ≥ p ≥ 2, so any crossed-out number is composite.
A composite m has a prime factor p ≤ √m ≤ √n by “Primes and Unique Factorisation”. When the outer loop reaches p, p is still a candidate (it is prime), and m = p·(m/p) with m/p ≥ p, so m ≥ p² and m is a multiple of p at or beyond p². It is crossed out.
□
The inner loop for prime p performs about n/p operations, so the total is roughly n Σ_{p ≤ √n} 1/p. The sum of reciprocals of primes up to x grows like ln ln x, giving Θ(n log log n) — very nearly linear, and log log n is under 5 for any n anyone will run.
Compare with testing each number by trial division: about √m divisions for each m, giving Θ(n^{3/2}/log n) overall. At n = 10⁶ the sieve does a few million operations and trial division does roughly a hundred million.
The prime number theorem says the count is asymptotically n / ln n, and the table shows the approximation improving slowly. A better estimate is the logarithmic integral, which at 10⁶ gives 78 628 against the true 78 498.
The sieve finds all primes up to n and needs memory proportional to n. For the 1024-bit primes RSA needs, sieving is hopeless: there are more candidates than atoms in the universe. Those primes are found by testing random numbers with probabilistic tests such as Miller–Rabin, which decide primality without producing factors.
Interactive
Algorithms with their traces
Method
One pass per prime, starting at its square.
2 through n. The number 1 is excluded from the start.
It is prime, because nothing smaller divides it.
Smaller multiples already went. This is the optimisation that halves the work at small primes.
Everything still uncrossed is prime, by the trial-division bound.
√30 ≈ 5.5, so only 2, 3 and 5 need passes.
The p = 5 pass crossed out exactly one number. That is typical: nearly all the work happens at the smallest primes, which is why the total cost is n log log n rather than n log n.
Before sieving to 200, say which primes need passes and roughly how many numbers each will cross out. Then run it and compare — the prediction is the understanding.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.