Möbius Inversion

Imagine you only ever get to see running totals. You don't know how much money went into your piggy bank each week — you only ever check the total sitting inside it. If someone handed you the weekly totals, could you work backwards and figure out exactly how much was added each week? With ordinary running totals, yes: just subtract consecutive totals. But number theory has its own strange kind of running total — a sum over divisors instead of over time — and subtracting doesn't work there at all.

Suppose a function g(n) is built by adding up another function f over every divisor of n. If all you're given is g, can you recover the original f exactly? Möbius inversion says yes — and it hands you a precise recipe, a genuine "un-sum" operation, built entirely out of the Möbius function \mu.

The setup

Suppose g is the divisor-sum of some function f:

g(n) = \sum_{d \mid n} f(d).

This kind of relationship is everywhere in number theory. For instance, the divisor-count function \tau and the divisor-sum function \sigma are both secretly running totals of much simpler functions in disguise. Knowing all the totals g(1), g(2), g(3), \dots, can we always recover f? It isn't obvious — a running total mixes together contributions from every divisor at once, so untangling one specific f(d) looks, at first glance, hopeless.

Compare it with something more familiar. If you know the partial sums s_1, s_2, s_3, \dots of an ordinary sequence (s_k = a_1 + a_2 + \cdots + a_k), recovering each a_k is easy: just subtract neighbours, a_k = s_k - s_{k-1}. A divisor-sum is the same idea — a "total built by adding up contributions" — except the contributions are indexed by divisors rather than by a simple 1-to-n count, and the divisors of n don't sit in a neat line you can subtract along. That's exactly the gap Möbius inversion fills.

The inversion formula

It isn't hopeless at all — it just needs the right weights. Multiply each g(n/d) by \mu(d) and add them up, and every unwanted term cancels, leaving exactly f(n) behind.

The sum runs over all the divisors d of n, exactly as it did when building g from f — the only new ingredient is the sign \mu(d) in front of each term, and evaluating g not at d but at the complementary divisor n/d.

Worked example: recovering f(n) = n from \sigma

Here's a clean place to see the formula do real work. The divisor-sum function σ(n) adds up all the divisors of n. That's exactly a divisor-sum of the simplest possible function, f(d) = d:

\sigma(n) = \sum_{d \mid n} d = \sum_{d \mid n} f(d), \qquad f(d) = d.

So if all we were handed was the table of \sigma-values, Möbius inversion promises we can rebuild f(n) = n — trivially simple, which makes it a perfect function to double-check the machinery against. Let's verify it for n = 6, whose divisors are 1, 2, 3, 6.

Step 1 — gather the ingredients. We need \sigma(6/d) for each divisor d, and \mu(d) for each one too:

\sigma(1)=1,\ \ \sigma(2)=3,\ \ \sigma(3)=4,\ \ \sigma(6)=12, \qquad \mu(1)=1,\ \ \mu(2)=-1,\ \ \mu(3)=-1,\ \ \mu(6)=1.

Step 2 — pair each divisor d with its partner n/d. For n = 6:

d=1 \Rightarrow n/d = 6,\quad d=2 \Rightarrow n/d = 3,\quad d=3 \Rightarrow n/d = 2,\quad d=6 \Rightarrow n/d = 1.

Step 3 — apply the formula.

f(6) = \mu(1)\sigma(6) + \mu(2)\sigma(3) + \mu(3)\sigma(2) + \mu(6)\sigma(1) = (1)(12) + (-1)(4) + (-1)(3) + (1)(1) = 12 - 4 - 3 + 1 = 6.

And f(6) = 6 is exactly right — the formula handed back n itself, with no knowledge of f beyond its divisor-sums. Try the calculation below with any n you like; it always comes back out to n.

function mu(n: number): number { if (n === 1) return 1; let m = n; let primes = 0; for (let p = 2; p * p <= m; p++) { if (m % p === 0) { let count = 0; while (m % p === 0) { m /= p; count++; } if (count > 1) return 0; primes++; } } if (m > 1) primes++; return primes % 2 === 0 ? 1 : -1; } function divisors(n: number): number[] { const ds: number[] = []; for (let d = 1; d <= n; d++) if (n % d === 0) ds.push(d); return ds; } function sigma(n: number): number { return divisors(n).reduce((sum, d) => sum + d, 0); } // Möbius inversion: recover f(n) = n from its divisor-sum g = sigma. function invert(n: number): number { return divisors(n).reduce((sum, d) => sum + mu(d) * sigma(n / d), 0); } for (const n of [6, 10, 12, 17]) { console.log(`f(${n}) recovered = ${invert(n)} (should equal ${n})`); }

Why it works: μ was built for exactly this

This isn't a coincidence — it's the entire reason μ was defined the way it was. Substitute the definition of g into the inversion formula and swap the order of summation:

\sum_{d \mid n} \mu(d)\, g(n/d) = \sum_{d \mid n} \mu(d) \sum_{e \mid (n/d)} f(e) = \sum_{e \mid n} f(e) \sum_{d \mid (n/e)} \mu(d).

Look at that inner sum: \sum_{d \mid (n/e)} \mu(d) is exactly the divisor-sum identity from the Möbius function page — it equals 1 when n/e = 1 (that is, e = n) and 0 for every other e. Every term in the outer sum vanishes except the one where e = n, which survives with coefficient 1 — leaving exactly f(n). The whole inversion formula is just the Möbius function's cancellation property, used once to sieve a single term out of a sum.

Another payoff: recovering the totient

A second classic application recovers Euler's totient. Because every number from 1 to n has a unique gcd with n, one can show \sum_{d \mid n} \varphi(d) = n. Möbius inversion turns this around into a formula for \varphi itself:

\varphi(n) = \sum_{d \mid n} \mu(d)\,\frac{n}{d} = n \prod_{p \mid n}\left(1 - \frac1p\right).

Notice the shape is identical to the worked example above — only the divisor-sum being inverted has changed, from \sigma to the identity function n itself (\sum_{d\mid n}\varphi(d)=n means g(n)=n here). The very same recipe cracks both.

The formula has two moving parts, and it's very easy to mix them up under pressure:

f(n) = \sum_{d \mid n} \mu(d)\, g\!\left(\frac{n}{d}\right).

A reliable habit: list the divisors of n in pairs (d,\, n/d) first — as we did for n=6 above, pairing 1\leftrightarrow 6 and 2 \leftrightarrow 3 — and only then multiply \mu of the first entry by g of the second.

Möbius inversion looks like a specialist gadget for number theory, but the same trick keeps resurfacing far outside it. Anywhere you can organise objects by a "divides" or "contains" relationship and count them with a running total, an analogous inversion formula tends to exist.

In combinatorics it powers the counting of necklaces — bracelets of coloured beads that look the same after rotating — and the count of irreducible polynomials over finite fields (the building blocks used in error-correcting codes that protect data on CDs, QR codes, and deep-space probes). In statistical physics, the very same alternating-sign cancellation shows up when physicists count distinct configurations of interacting particles while correcting for overlapping possibilities — a direct cousin of inclusion–exclusion wearing a different costume. The general theory, called Möbius functions on partially ordered sets, treats divisibility as just one example of a much broader pattern — number theory's \mu was simply the first one anybody wrote down.