Proofs: Möbius sieve
These are the human proofs of Möbius sieve, at full length.
The Möbius sieve computes mu(m) for every m up to n full
This proof shows: correctness.
For let be the least prime factor of . Lines L1 to L6 and the list of primes are exactly the linear sieve, and lines L3, L7 and L8 write only to , which the linear sieve never reads. So the proof of the linear sieve applies without change, and gives these facts for :
- In the round of , after line L3, , and line L3 runs its body exactly when is prime.
- Line L6 runs exactly once for each composite , in the round of , with , and for no other index.
Claim. For each with , the entry is written exactly once after line L1 sets it, and it then gets the value . More precisely: is set to 1 in line L1 and never changed; for a prime the only write is in line L3; for a composite the only write is in line L7 or line L8 of the round .
Proof. Lines L3, L7 and L8 write at or at , so keeps the value 1 . A prime is not of the form with , so lines L7 and L8 never write it, and by fact 1 line L3 writes in the round of . Line L3 never writes a composite, by fact 1. A composite is written by lines L7 or L8 only together with line L6, so by fact 2 exactly once, in the round of .
It remains to check the value for composite , by strong induction on . Let and , so and . The entry was set before the round of reaches line L4: in line L1 if (impossible here), in line L3 of the same round if is prime, or in the round of if is composite. By induction it equals , and it does not change later. By fact 1, .
- If , then , so and . Line L7 writes 0.
- If , then does not divide , since every prime factor of is at least . So , and because is multiplicative, . Line L8 writes .
In both cases the written value is .
For the output is , and for it is ; both are correct, since the loop of line L2 does not run. Index 0 is never written after line L1, so . The loops terminate as in the linear sieve. By the claim, the array returned in line L9 holds for every .
Multiplicativity of . The step for follows from the definition: if is squarefree with distinct prime factors, then is squarefree with , so ; if has a square factor, so does , and both values are 0.