Algorithm Search⌘K

Proofs: Sieve of Atkin

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

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

This proof shows: correctness.

We use the three theorems of Atkin and Bernstein (Theorems 6.1, 6.2 and 6.3 of Prime sieves using binary quadratic forms), which hold for every squarefree positive integer mm:

  • (A) if m≡1(mod4)m \equiv 1 \pmod 4: mm is prime   ⟺  \iff N1(m)=#{(x,y):x,y>0, 4x2+y2=m}N_1(m) = \#\{(x, y) : x, y > 0,\ 4x^2 + y^2 = m\} is odd;
  • (B) if m≡1(mod6)m \equiv 1 \pmod 6: mm is prime   ⟺  \iff N2(m)=#{(x,y):x,y>0, 3x2+y2=m}N_2(m) = \#\{(x, y) : x, y > 0,\ 3x^2 + y^2 = m\} is odd;
  • (C) if m≡11(mod12)m \equiv 11 \pmod{12}: mm is prime   ⟺  \iff N3(m)=#{(x,y):x>y>0, 3x2−y2=m}N_3(m) = \#\{(x, y) : x > y > 0,\ 3x^2 - y^2 = m\} is odd.

Their proofs count ideals of norm mm in Z[i]\mathbb{Z}[i], Z[ω]\mathbb{Z}[\omega] and Z[3]\mathbb{Z}[\sqrt 3]: there are 2 for a prime and a multiple of 4 for a product of at least two distinct primes. Here we prove that the algorithm is correct given (A), (B) and (C).

If n<2n < 2 there is no prime up to nn, and line L1 returns the empty list. Assume n≥2n \ge 2.

Classes. An integer mm is coprime to 6 if and only if m mod 12∈{1,5,7,11}m \bmod 12 \in \{1, 5, 7, 11\}. For such mm define its count N(m)=N1(m)N(m) = N_1(m) if m mod 12∈{1,5}m \bmod 12 \in \{1, 5\}, N(m)=N2(m)N(m) = N_2(m) if m mod 12=7m \bmod 12 = 7, and N(m)=N3(m)N(m) = N_3(m) if m mod 12=11m \bmod 12 = 11. In the first case m≡1(mod4)m \equiv 1 \pmod 4, in the second m≡1(mod6)m \equiv 1 \pmod 6, in the third m≡11(mod12)m \equiv 11 \pmod{12}, so by (A), (B) and (C):

(*) for every squarefree mm coprime to 6, mm is prime if and only if N(m)N(m) is odd.

After lines L3 to L5. Line L2 sets all flags to false. Line L3 visits every pair x,y≥1x, y \ge 1 with 4x2+y2≤n4x^2 + y^2 \le n exactly once, and flips F[m]F[m] for m=4x2+y2m = 4x^2 + y^2 when m mod 12∈{1,5}m \bmod 12 \in \{1, 5\}. So the number of flips it makes at a given m≤nm \le n is N1(m)N_1(m) if m mod 12∈{1,5}m \bmod 12 \in \{1, 5\} and 0 otherwise. In the same way line L4 flips mm exactly N2(m)N_2(m) times if m mod 12=7m \bmod 12 = 7 and never otherwise, and line L5 flips mm exactly N3(m)N_3(m) times if m mod 12=11m \bmod 12 = 11 and never otherwise. (In line L5, for fixed xx the value 3x2−y23x^2 - y^2 decreases in yy, so the pairs with y<xy < x and value at most nn are those with yy from x−1x - 1 down to the first yy whose value exceeds nn, exclusive. If some y<xy < x qualifies, then so does y=x−1y = x - 1, the smallest value, and 3x2−(x−1)2=2x2+2x−1≤n3x^2 - (x - 1)^2 = 2x^2 + 2x - 1 \le n; this is the bound on xx.) A flag that starts false and is flipped tt times is true if and only if tt is odd. Hence, after line L5, for 0≤m≤n0 \le m \le n:

(**) F[m]F[m] is true if and only if mm is coprime to 6 and N(m)N(m) is odd.

In particular F[m]F[m] is false for m∈{0,1,2,3,4}m \in \{0, 1, 2, 3, 4\}: 0,2,3,40, 2, 3, 4 are not coprime to 6, and N(1)=N1(1)=0N(1) = N_1(1) = 0, since 4x2+y2≥54x^2 + y^2 \ge 5 for x,y≥1x, y \ge 1.

Lines L6 and L7. We show by induction on r=5,6,…,⌊n⌋r = 5, 6, \ldots, \lfloor \sqrt n \rfloor that when line L7 tests F[r]F[r], the flag is true if and only if rr is prime. Lines L6 and L7 only ever set flags to false.

Suppose that for every 5≤s<r5 \le s < r the test of F[s]F[s] was true exactly when ss was prime; then every multiple of q2q^2 up to nn has been cleared for each prime 5≤q<r5 \le q < r. Consider F[r]F[r].

  • If rr is prime (r≥5r \ge 5), then rr is coprime to 6 and squarefree, and N(r)N(r) is odd by (*); so F[r]F[r] was true after line L5 by (**). It was not cleared since: a clearing at rr would need q2∣rq^2 \mid r for some q<rq < r, which a prime does not allow. So F[r]F[r] is true.
  • If rr is not coprime to 6, F[r]F[r] was false after line L5 and stays false.
  • If rr is coprime to 6 and squarefree but composite, N(r)N(r) is even by (*), so F[r]F[r] is false by (**).
  • If rr is coprime to 6 and not squarefree, some prime qq has q2∣rq^2 \mid r; q≠2,3q \ne 2, 3, so q≥5q \ge 5, and q<rq < r. By the induction hypothesis F[q]F[q] was true when tested, so line L7 cleared all multiples of q2q^2 up to nn, including rr.

So the claim holds for rr. Consequently, after the loop of line L6, the flags cleared by lines L6 and L7 are exactly the multiples of p2p^2 up to nn for the primes 5≤p≤n5 \le p \le \sqrt n.

Line L8. Let 5≤m≤n5 \le m \le n. If mm is prime, it is coprime to 6 and squarefree, so F[m]F[m] is true after line L5 by (*) and (**), and no p2p^2 with pp prime divides it, so it is not cleared: mm is output. If mm is not prime, either mm is not coprime to 6, and F[m]F[m] was never true; or mm is coprime to 6 and squarefree, and N(m)N(m) is even, so F[m]F[m] was never true; or mm is coprime to 6 and not squarefree, so p2∣mp^2 \mid m for a prime p≥5p \ge 5 with p2≤m≤np^2 \le m \le n, hence p≤⌊n⌋p \le \lfloor \sqrt n \rfloor, and mm was cleared in line L7. So the numbers m≥5m \ge 5 output are exactly the primes in [5,n][5, n], in increasing order, and line L8 puts the primes 2 and 3 that are at most nn before them. The output is the increasing list of all primes up to nn. ■\blacksquare

Back to Sieve of Atkin