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 :
- (A) if : is prime is odd;
- (B) if : is prime is odd;
- (C) if : is prime is odd.
Their proofs count ideals of norm in , and : 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 there is no prime up to , and line L1 returns the empty list. Assume .
Classes. An integer is coprime to 6 if and only if . For such define its count if , if , and if . In the first case , in the second , in the third , so by (A), (B) and (C):
(*) for every squarefree coprime to 6, is prime if and only if is odd.
After lines L3 to L5. Line L2 sets all flags to false. Line L3 visits every pair with exactly once, and flips for when . So the number of flips it makes at a given is if and 0 otherwise. In the same way line L4 flips exactly times if and never otherwise, and line L5 flips exactly times if and never otherwise. (In line L5, for fixed the value decreases in , so the pairs with and value at most are those with from down to the first whose value exceeds , exclusive. If some qualifies, then so does , the smallest value, and ; this is the bound on .) A flag that starts false and is flipped times is true if and only if is odd. Hence, after line L5, for :
(**) is true if and only if is coprime to 6 and is odd.
In particular is false for : are not coprime to 6, and , since for .
Lines L6 and L7. We show by induction on that when line L7 tests , the flag is true if and only if is prime. Lines L6 and L7 only ever set flags to false.
Suppose that for every the test of was true exactly when was prime; then every multiple of up to has been cleared for each prime . Consider .
- If is prime (), then is coprime to 6 and squarefree, and is odd by (*); so was true after line L5 by (**). It was not cleared since: a clearing at would need for some , which a prime does not allow. So is true.
- If is not coprime to 6, was false after line L5 and stays false.
- If is coprime to 6 and squarefree but composite, is even by (*), so is false by (**).
- If is coprime to 6 and not squarefree, some prime has ; , so , and . By the induction hypothesis was true when tested, so line L7 cleared all multiples of up to , including .
So the claim holds for . Consequently, after the loop of line L6, the flags cleared by lines L6 and L7 are exactly the multiples of up to for the primes .
Line L8. Let . If is prime, it is coprime to 6 and squarefree, so is true after line L5 by (*) and (**), and no with prime divides it, so it is not cleared: is output. If is not prime, either is not coprime to 6, and was never true; or is coprime to 6 and squarefree, and is even, so was never true; or is coprime to 6 and not squarefree, so for a prime with , hence , and was cleared in line L7. So the numbers output are exactly the primes in , in increasing order, and line L8 puts the primes 2 and 3 that are at most before them. The output is the increasing list of all primes up to .