Number TheoryFermat, Euler and Wilson

Euler's Phi Function

φ(n) counts the k from 1 to n with gcd(k, n) = 1; φ(pᵏ) = pᵏ − pᵏ⁻¹, and φ(mn) = φ(m)φ(n) when gcd(m, n) = 1.

Not started Jump to practice

Mod a prime $p$, all $p - 1$ nonzero residues have inverses. Mod a composite, only some do: the ones coprime to $n$. Counting them is the job of Euler's phi function, and the count is what replaces $p - 1$ when we generalize Fermat's theorem.

Definition. $\varphi(n)$ is the number of integers $k$ with $1 \le k \le n$ and $\gcd(k, n) = 1$. For example, mod 12 the numbers coprime to 12 are $1, 5, 7, 11$, so $\varphi(12) = 4$. These are the units mod $n$, the residues with inverses.

Formulas. For a prime $p$: $\varphi(p) = p - 1$ and $\varphi(p^{k}) = p^{k} - p^{k-1}$. If $\gcd(m, n) = 1$, then $\varphi(mn) = \varphi(m)\varphi(n)$. Together, with the prime factorization of $n$: $$\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right).$$

Why it works

Prime powers. Among $1, \dots, p^{k}$, a number shares a factor with $p^{k}$ exactly when it is a multiple of $p$, and there are $p^{k-1}$ multiples of $p$. Remove them: $p^{k} - p^{k-1}$ remain.
Coprime products. By the Chinese Remainder Theorem, each $x$ mod $mn$ matches exactly one pair ($x \bmod m$, $x \bmod n$), and $x$ is coprime to $mn$ exactly when both parts are coprime to their moduli. So the units mod $mn$ match the pairs of units, and there are $\varphi(m)\varphi(n)$ pairs.
The units mod 12 match the pairs (unit mod 4, unit mod 3): $2 \cdot 2 = 4$ of them.
$k$ mod 1215711
mod 41133
mod 31212

Worked example: A prime power times another

Find $\varphi(36)$.
  1. $36 = 2^{2} \cdot 3^{2}$, with coprime factors $4$ and $9$.
  2. $\varphi(4) = 4 - 2 = 2$ and $\varphi(9) = 9 - 3 = 6$.
  3. $\varphi(36) = 2 \cdot 6 = 12$. Same by the product formula: $36 \cdot \frac{1}{2} \cdot \frac{2}{3} = 12$.

Answer: $12$.

Worked example: Three primes

Find $\varphi(1001)$.
  1. $1001 = 7 \cdot 11 \cdot 13$.
  2. $\varphi(1001) = 6 \cdot 10 \cdot 12 = 720$.

Answer: $720$.

Worked example: Fractions in lowest terms

How many of the fractions $\frac{1}{12}, \frac{2}{12}, \dots, \frac{12}{12}$ are already in lowest terms?
  1. $\frac{k}{12}$ is in lowest terms exactly when $\gcd(k, 12) = 1$.
  2. So the count is $\varphi(12) = 4$: $\frac{1}{12}, \frac{5}{12}, \frac{7}{12}, \frac{11}{12}$.

Answer: $4$.

The first values. $\varphi(n) = n - 1$ exactly when $n$ is prime, and $\varphi(n)$ is even for every $n \ge 3$.
$n$123456789101112
$\varphi(n)$1122426464104

Common mistake

Multiplying $\varphi$ values of factors that share a prime. $\varphi(4) = 2$ (the units are 1 and 3), but $\varphi(2) \cdot \varphi(2) = 1 \cdot 1 = 1$. The rule $\varphi(mn) = \varphi(m)\varphi(n)$ needs $\gcd(m, n) = 1$. Split $n$ into prime powers first, and use $p^{k} - p^{k-1}$ on each.
A pretty identity: adding $\varphi(d)$ over all divisors $d$ of $n$ gives $n$. For $n = 12$: $1 + 1 + 2 + 2 + 2 + 4 = 12$ (for $d = 1, 2, 3, 4, 6, 12$). It counts the fractions $\frac{k}{12}$ by their reduced denominators.

Key idea

$\varphi(n)$ counts the units mod $n$. Factor $n$ into prime powers; $\varphi(p^{k}) = p^{k} - p^{k-1}$, and multiply.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 2, The Ring of Integers Modulo n (Euler's phi function)
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: Euler's theorem
@teddyrookbook GitHub