Proofs: Divisor-count sieve
These are the human proofs of Divisor-count sieve, at full length.
The divisor-count sieve computes d(m) for every m up to n full
This proof shows: correctness.
Fix with . Line L1 sets , and line L4 is the only line that changes ; each run adds 1. So at the end, equals the number of pairs for which line L4 runs with , that is, with the value of the outer loop and the step of the inner loop.
Claim. Line L4 runs at the index exactly once for each positive divisor of , and never for any other .
Proof. For a fixed with , the loop of line L3 takes the values while . These values are distinct, and they are exactly the multiples of in . So the index appears in the round of once if , and not at all otherwise. Every positive divisor of satisfies , so the loop of line L2 reaches it.
By the claim, ends equal to the number of positive divisors of , which is . No run of line L4 writes index 0, since , so . The loops terminate: the outer loop runs times, and the round of runs times.
The same argument, with line L4 adding instead of 1, shows that the variant computes .
The count of runs. By the claim, the total number of runs of line L4 is , and counting by instead gives . These two sums are equal, which is the identity used in the complexity section.