units-mod-n-coprime-and-totient

Sun Sep 13 2026

A residue aa has a multiplicative inverse mod nn exactly when the greatest common divisor (gcd) of aa and nn is 1. The invertible residues form a group, the unit group (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times, and it has φ(n)\varphi(n) elements.

Start with n = 6

Which of 1 to 5 have an inverse mod 6? Multiply and look for a 1. Since 1⋅1=11 \cdot 1 = 1 and 5⋅5=25≡15 \cdot 5 = 25 \equiv 1, both 1 and 5 are units. The residue 1 is a unit for every nn and is its own inverse, since gcd⁡(1,n)=1\gcd(1, n) = 1. The other three never reach 1: 2⋅3=6≡02 \cdot 3 = 6 \equiv 0, 3⋅2≡03 \cdot 2 \equiv 0, 4⋅3=12≡04 \cdot 3 = 12 \equiv 0. Once a product hits 0 the row of products just cycles.

The rule is not "nn divides aa": 6 does not divide 4, and 4 still fails. The rule is that aa and nn share a factor. 4 shares the factor 2 with 6, and a shared factor is enough to kill the inverse.

An inverse is a combination equal to 1

Write out what an inverse means. ak≡1(modn)a k \equiv 1 \pmod n says ak=1+qna k = 1 + q n for some integer qq, so

ak−qn=1.a k - q n = 1 .

So "does aa have an inverse" is the same question as "can I build 1 out of aa and nn with integer coefficients". Bézout's identity is the name for the fact that the smallest positive number you can build this way is gcd⁡(a,n)\gcd(a, n). The two directions below are that identity, proved on the pair we care about.

Shared factor means no inverse

Let d=gcd⁡(a,n)d = \gcd(a, n) and write a=k1da = k_1 d, n=k2dn = k_2 d. Then every combination is

ak−nq=(kk1−qk2) d.a k - n q = (k k_1 - q k_2)\, d .

Every combination is a whole number of copies of dd. If d>1d > 1 the combination can never equal 1, so there is no inverse. That is the "only if" direction.

Coprime means an inverse exists: run Euclid backwards

Take a=5a = 5 and n=12n = 12. Divide with remainder, then divide the divisor by the remainder, until the remainder is 0:

12=2⋅5+25=2⋅2+12=2⋅1+0.\begin{aligned} 12 &= 2 \cdot 5 + 2 \\ 5 &= 2 \cdot 2 + 1 \\ 2 &= 2 \cdot 1 + 0 . \end{aligned}

The last nonzero remainder is 1, and the goal is to write that 1 as a combination of 12 and 5. The first equation already does this for its own remainder: 2=12−2⋅52 = 12 - 2 \cdot 5. The second equation contains that 2, so substitute, and its remainder 1 becomes a combination of 12 and 5 as well:

1=5−2⋅2=5−2 (12−2⋅5)=5⋅5−12⋅2.\begin{aligned} 1 &= 5 - 2 \cdot 2 \\ &= 5 - 2\,(12 - 2 \cdot 5) \\ &= 5 \cdot 5 - 12 \cdot 2 . \end{aligned}

Reduce mod 12 and the 12⋅212 \cdot 2 term vanishes: 5⋅5≡15 \cdot 5 \equiv 1, so 5 is its own inverse mod 12.

The general idea is a chain of substitutions. The first equation holds aa, nn and a remainder. The remainder has coefficient 1 in its own equation, so solving for it writes the remainder as a combination of aa and nn. Substitute that into the next equation, and its remainder is a combination of aa and nn too. Repeat down the chain. The last equation then reads gcd⁡(a,n)=ax+ny\gcd(a, n) = a x + n y, and for coprime aa and nn that is 1=ax+ny1 = a x + n y, with xx the inverse. This is the extended Euclidean algorithm. Tracking xx and yy step by step, rather than by substitution, is the follow-up in extended-euclidean-algorithm-study.

Two claims carry this proof and both need checking: each Euclid step keeps the gcd, and the chain stops exactly at the gcd.

Each Euclid step keeps the gcd

One step is a=qb+ra = q b + r with 0≤r<b0 \le r < b. The claim is gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r).

units-mod-n-division-strip

First half. Let d=gcd⁡(a,b)d = \gcd(a, b), so a=k1da = k_1 d and b=k2db = k_2 d. Picture a strip of length aa cut into qq segments of length bb and a leftover of length rr. The block dd tiles the whole strip and tiles each bb segment, so it must tile the leftover:

r=a−qb=(k1−qk2) d.r = a - q b = (k_1 - q k_2)\, d .

So dd is a common divisor of bb and rr, and that is all it is so far. The block dd is the greatest common divisor of (a,b)(a, b). Nothing above says it is the greatest common divisor of (b,r)(b, r), only that it is a factor of both. Being the gcd of the first pair does not make it the gcd of the second. What this half proves is gcd⁡(a,b)≤gcd⁡(b,r)\gcd(a, b) \le \gcd(b, r).

Second half. Take any ee dividing both bb and rr, say b=em1b = e m_1 and r=em2r = e m_2. Then

a=qb+r=e (qm1+m2),a = q b + r = e\,(q m_1 + m_2),

so ee divides aa as well. Every common divisor of (b,r)(b, r) is a common divisor of (a,b)(a, b), which gives gcd⁡(b,r)≤gcd⁡(a,b)\gcd(b, r) \le \gcd(a, b). The two inequalities together give equality. The trap in the one-sided version is its own note: euclid-step-keeps-gcd.

Why the chain stops at the gcd

The remainders are non-negative and strictly decreasing, since r<br < b at every step, so the chain reaches 0 after finitely many steps. The last line reads rk−1=q⋅rk+0r_{k-1} = q \cdot r_k + 0, and gcd⁡(rk,0)=rk\gcd(r_k, 0) = r_k, because every integer divides 0. Chain the lemma back up the lines and gcd⁡(a,n)=gcd⁡(rk,0)=rk\gcd(a, n) = \gcd(r_k, 0) = r_k. The last nonzero remainder is the gcd. When aa and nn are coprime that remainder is 1, which is exactly what the back-substitution builds.

The answer to the opening question

For prime pp the only divisors are 1 and pp, so every residue from 1 to p−1p - 1 is coprime to pp and has an inverse. That is why Zp\mathbb{Z}_p is a field. For composite nn, exactly the residues that share a factor with nn drop out. Mod 6, that is 2, 3 and 4, and the survivors are 1 and 5.

The units form a group

Closure is the claim to check: if aa and bb are units, so is aba b. Two different closures are in play, and keeping them apart is the whole proof.

The first is closure of the ring. Both a−1a^{-1} and b−1b^{-1} are elements of Z/nZ\mathbb{Z}/n\mathbb{Z}, and a ring is closed under multiplication, so the product b−1a−1b^{-1} a^{-1} is guaranteed to exist as an element of Z/nZ\mathbb{Z}/n\mathbb{Z}. The ring hands over the candidate inverse for free.

The second is closure of the unit group, which is the thing to prove. A unit is an element with some inverse in the ring, so check the candidate:

(ab)(b−1a−1)=a (b b−1) a−1=a a−1=1.\begin{aligned} (a b)(b^{-1} a^{-1}) &= a\,(b\, b^{-1})\,a^{-1} \\ &= a\, a^{-1} = 1 . \end{aligned}

So aba b has an inverse in the ring, which makes aba b a unit. That is unit-group closure, proved from ring closure plus one computation. Nothing here needed b−1a−1b^{-1} a^{-1} to be a unit itself. Reaching for that fact is the circular step, because it is unit-group closure again.

The identity is 1, inverses exist by the definition of a unit, and associativity is inherited from the ring. So (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times is a group.

How many units: φ(n)

Euler's totient φ(n)\varphi(n) counts the integers in 11 to nn that are coprime to nn, so ∣(Z/nZ)×∣=φ(n)|(\mathbb{Z}/n\mathbb{Z})^\times| = \varphi(n), and φ(p)=p−1\varphi(p) = p - 1 for a prime.

Count n=15=3⋅5n = 15 = 3 \cdot 5 by removing the failures. Count over 1 to 15 rather than 1 to 14, so that every count is exact and 15 itself lands in the removed pile where it belongs. The multiples of 3 number 15/3=515/3 = 5, the multiples of 5 number 15/5=315/5 = 3, and 15 was removed twice, so add it back once:

φ(15)=15−153−155+1515=8.\varphi(15) = 15 - \tfrac{15}{3} - \tfrac{15}{5} + \tfrac{15}{15} = 8 .

The eight units mod 15 are 1, 2, 4, 7, 8, 11, 13, 14. The same count for any two distinct primes gives φ(pq)=pq−q−p+1=(p−1)(q−1)\varphi(pq) = pq - q - p + 1 = (p - 1)(q - 1).

The general formula runs over the distinct primes dividing nn:

φ(n)=n∏p∣n(1−1p).\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right).

The way to remember it: for each prime pp there are exactly n/pn/p multiples to throw out. Start from nn, subtract each n/pn/p, add back each n/(pq)n/(pq) that was subtracted twice, and so on. Multiplying out the product produces exactly those signed terms, one per subset of the primes. Check: 15⋅23⋅45=815 \cdot \tfrac{2}{3} \cdot \tfrac{4}{5} = 8. For 99 there is only one prime and nothing to double count, so φ(9)=9−3=6\varphi(9) = 9 - 3 = 6.

Mantra The inverse question is a gcd question, and the gcd question is Euclid.

Open threads

  • φ(pk)\varphi(p^k), with 27 as the test case: which residues fail, how many, and the closed form.
  • The term-by-term match between expanding ∏(1−1/pi)\prod (1 - 1/p_i) and inclusion-exclusion for three or more primes. Sketched in the session, not re-derived.
  • Tracking the Bézout coefficients step by step instead of by substitution: extended-euclidean-algorithm-study.