Algorithm Search⌘K

Proofs: Sieve of Eratosthenes

These are the human proofs of Sieve of Eratosthenes, at full length.

The sieve of Eratosthenes returns exactly the primes up to n full

This proof shows: correctness.

For n<2n < 2 there is no prime p≤np \le n, and line L1 returns the empty list. From now on n≥2n \ge 2. For an integer m≥2m \ge 2 let lpf(m)\mathrm{lpf}(m) be its smallest prime factor. We use two facts.

Fact 1. If m≥2m \ge 2 is composite, then lpf(m)2≤m\mathrm{lpf}(m)^2 \le m. Indeed m=qkm = q k with q=lpf(m)q = \mathrm{lpf}(m) and k≥2k \ge 2. Each prime factor of kk divides mm, so it is at least qq, and therefore k≥qk \ge q and m=qk≥q2m = qk \ge q^2.

Fact 2. If m≥2m \ge 2 is composite and q=lpf(m)q = \mathrm{lpf}(m), then m=qkm = q k with k≥qk \ge q. This is the same argument.

Invariant. Each time line L4 tests a value pp, the following holds for every mm with 2≤m≤n2 \le m \le n:

(I) P[m]P[m] is false if and only if mm is composite and lpf(m)<p\mathrm{lpf}(m) < p.

Initialization. Line L3 sets p=2p = 2. Line L2 sets every P[m]P[m] with m≥2m \ge 2 to true, and no mm has lpf(m)<2\mathrm{lpf}(m) < 2. So (I) holds.

Line L5 tests primality. Suppose line L4 lets pp through, so p2≤np^2 \le n and 2≤p≤n2 \le p \le n. If pp is composite, then by Fact 1, lpf(p)≤p<p\mathrm{lpf}(p) \le \sqrt p < p, so by (I), P[p]P[p] is false. If pp is prime, then pp is not composite, so by (I), P[p]P[p] is true. Hence the test of line L5 succeeds exactly when pp is prime.

Maintenance. We show that (I) holds for p+1p + 1 after line L8.

  • If pp is composite, lines L6 and L7 do not run and no flag changes. No integer mm has lpf(m)=p\mathrm{lpf}(m) = p, since lpf(m)\mathrm{lpf}(m) is prime. So "mm composite and lpf(m)<p\mathrm{lpf}(m) < p" is the same as "mm composite and lpf(m)<p+1\mathrm{lpf}(m) < p + 1", and (I) holds for p+1p + 1.
  • If pp is prime, lines L6 and L7 set P[m]P[m] to false for exactly the numbers m=pkm = p k with k≥pk \ge p and m≤nm \le n. Each such mm is composite, because k≥p≥2k \ge p \ge 2, and lpf(m)≤p\mathrm{lpf}(m) \le p. Conversely, let m≤nm \le n be composite with lpf(m)=p\mathrm{lpf}(m) = p. By Fact 2, m=pkm = p k with k≥pk \ge p, so the loop clears P[m]P[m]. No flag is set back to true. So after the loop, P[m]P[m] is false exactly when mm is composite with lpf(m)<p\mathrm{lpf}(m) < p (cleared before, by (I)) or composite with lpf(m)=p\mathrm{lpf}(m) = p (cleared now). This is (I) for p+1p + 1.

Termination. The value pp grows by one in each pass of the loop, so the test p⋅p≤np \cdot p \le n of line L4 fails after at most n\sqrt n passes. Each loop of line L6 is finite, since mm grows by p≥2p \ge 2 and stops above nn.

Conclusion. When line L4 fails, p2>np^2 > n, so p>np > \sqrt n. Let 2≤m≤n2 \le m \le n. If mm is prime, it is not composite, so by (I), P[m]P[m] is true. If mm is composite, then by Fact 1, lpf(m)≤m≤n<p\mathrm{lpf}(m) \le \sqrt m \le \sqrt n < p, so by (I), P[m]P[m] is false. So P[m]P[m] is true exactly for the primes mm in [2,n][2, n], and line L9 returns all primes up to nn in increasing order. ■\blacksquare

The count of writes. For a prime pp with p2≤np^2 \le n, the loop of line L6 visits m=pkm = p k for k=p,p+1,…,⌊n/p⌋k = p, p + 1, \ldots, \lfloor n/p \rfloor, which is ⌊n/p⌋−p+1\lfloor n/p \rfloor - p + 1 values. Summed over these primes, this is the formula for W(n)W(n) in the complexity section. The lower bound uses ⌊n/p⌋−p+1≥n/p−p\lfloor n/p \rfloor - p + 1 \ge n/p - p and ∑p≤np≤n⋅n=n\sum_{p \le \sqrt n} p \le \sqrt n \cdot \sqrt n = n, so W(n)≥n∑p≤n1/p−nW(n) \ge n \sum_{p \le \sqrt n} 1/p - n, and Mertens' theorem makes this nln⁡ln⁡n−O(n)n \ln \ln n - O(n).

Back to Sieve of Eratosthenes