Proofs: Linear sieve
These are the human proofs of Linear sieve, at full length.
The linear sieve computes the primes and the least prime factors up to n, writing each composite once full
This proof shows: correctness.
For let be the least prime factor of . If , the loop of line L2 does not run, and the output is the empty list with for , which is correct. Let .
Lemma 1 (unique form). Let be composite. Among all ways to write with prime and , exactly one has , namely and . Moreover, for every prime and every with , we have .
Proof. Take and a prime . Every prime factor of is or a prime factor of , which is at least ; so . This is the second statement. For the first, if with , then by the second statement and , so there is at most one such form. And the form exists: with and , we have because is composite, and every prime factor of divides , so it is at least ; thus .
Invariant. When the loop of line L2 starts the round of (with , the value meaning the end):
(A) for , , and the list holds exactly the primes less than , in increasing order;
(B) for , if and only if is composite and , and then ;
(C) every write of line L7 so far went to a different index.
Initialization. At , the list is empty and every entry is 0. No composite has . No write has happened. So (A), (B) and (C) hold.
Maintenance. Assume the invariant at the start of the round of .
First, lines L3 and L4. By (B) with : if is composite, then , so and line L3 fails. If is prime, then , and line L4 sets and appends , which is larger than every prime in the list. Either way, after line L4, and the list holds exactly the primes up to , in increasing order.
Next, the loop of lines L5 to L7. Every prime satisfies , so it is in the list. The loop takes the primes of the list in increasing order and stops at the first one with or . So it runs line L7 exactly for the primes with and . By Lemma 1, line L7 sets , and .
Now check the invariant at . (A): the entry is correct, as shown, and entries below do not change, because each write goes to . (B): let . If is composite with , then already by (B), and a write in this round would go to with , which is not the case, so the entry stays. If is composite with , then by Lemma 1, and , so this round writes . In every other case, is prime or ; then by (B), and this round does not write , since each write goes to an with . (C): the writes of this round go to the indices for distinct , which are distinct, and each has . A write of an earlier round went to an index with . So no index is written twice.
Termination. The outer loop runs times, and each inner loop runs at most times.
Conclusion. At the end, , and (A) says that for all and that the list holds exactly the primes up to , in increasing order. Indices 0 and 1 are never written, since each write goes to and line L4 writes .
The count of writes. By (C) and (B), line L7 writes each composite exactly once, in the round of , and writes nothing else. So it runs exactly times. In each round, the inner loop makes one test of line L6 per write, plus at most one test that breaks or the end of the list, so lines L5 and L6 run at most times in total. This gives the bound.