Algorithm Search⌘K

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 m≥2m \ge 2 let lp(m)\mathrm{lp}(m) be the least prime factor of mm. Lines L1 to L6 and the list of primes are exactly the linear sieve, and lines L3, L7 and L8 write only to μ\mu, which the linear sieve never reads. So the proof of the linear sieve applies without change, and gives these facts for n≥2n \ge 2:

  1. In the round of ii, after line L3, lp[i]=lp(i)\mathrm{lp}[i] = \mathrm{lp}(i), and line L3 runs its body exactly when ii is prime.
  2. Line L6 runs exactly once for each composite m≤nm \le n, in the round of i=m/lp(m)i = m / \mathrm{lp}(m), with p=lp(m)≤lp(i)p = \mathrm{lp}(m) \le \mathrm{lp}(i), and for no other index.

Claim. For each mm with 1≤m≤n1 \le m \le n, the entry μ[m]\mu[m] is written exactly once after line L1 sets it, and it then gets the value μ(m)\mu(m). More precisely: μ[1]\mu[1] is set to 1 in line L1 and never changed; for a prime mm the only write is in line L3; for a composite mm the only write is in line L7 or line L8 of the round i=m/lp(m)i = m / \mathrm{lp}(m).

Proof. Lines L3, L7 and L8 write at i≥2i \ge 2 or at p⋅i≥4p \cdot i \ge 4, so μ[1]\mu[1] keeps the value 1 =μ(1)= \mu(1). A prime mm is not of the form p⋅ip \cdot i with i≥2i \ge 2, so lines L7 and L8 never write it, and by fact 1 line L3 writes μ[m]=−1=μ(m)\mu[m] = -1 = \mu(m) in the round of mm. Line L3 never writes a composite, by fact 1. A composite mm is written by lines L7 or L8 only together with line L6, so by fact 2 exactly once, in the round of i=m/lp(m)i = m / \mathrm{lp}(m).

It remains to check the value for composite mm, by strong induction on mm. Let p=lp(m)p = \mathrm{lp}(m) and i=m/pi = m / p, so 2≤i<m2 \le i < m and p≤lp(i)p \le \mathrm{lp}(i). The entry μ[i]\mu[i] was set before the round of ii reaches line L4: in line L1 if i=1i = 1 (impossible here), in line L3 of the same round if ii is prime, or in the round of i/lp(i)<ii / \mathrm{lp}(i) < i if ii is composite. By induction it equals μ(i)\mu(i), and it does not change later. By fact 1, lp[i]=lp(i)\mathrm{lp}[i] = \mathrm{lp}(i).

  • If p=lp(i)p = \mathrm{lp}(i), then p∣ip \mid i, so p2∣pi=mp^2 \mid p i = m and μ(m)=0\mu(m) = 0. Line L7 writes 0.
  • If p<lp(i)p < \mathrm{lp}(i), then pp does not divide ii, since every prime factor of ii is at least lp(i)\mathrm{lp}(i). So gcd⁡(p,i)=1\gcd(p, i) = 1, and because μ\mu is multiplicative, μ(m)=μ(p)μ(i)=−μ(i)\mu(m) = \mu(p) \mu(i) = -\mu(i). Line L8 writes −μ[i]=−μ(i)-\mu[i] = -\mu(i).

In both cases the written value is μ(m)\mu(m). □\square

For n=0n = 0 the output is [0][0], and for n=1n = 1 it is [0,1][0, 1]; both are correct, since the loop of line L2 does not run. Index 0 is never written after line L1, so μ[0]=0\mu[0] = 0. The loops terminate as in the linear sieve. By the claim, the array returned in line L9 holds μ(m)\mu(m) for every 1≤m≤n1 \le m \le n. ■\blacksquare

Multiplicativity of μ\mu. The step μ(pi)=μ(p)μ(i)\mu(p i) = \mu(p) \mu(i) for gcd⁡(p,i)=1\gcd(p, i) = 1 follows from the definition: if ii is squarefree with rr distinct prime factors, then pip i is squarefree with r+1r + 1, so μ(pi)=(−1)r+1=−μ(i)\mu(p i) = (-1)^{r+1} = -\mu(i); if ii has a square factor, so does pip i, and both values are 0.

Back to Möbius sieve