Number TheoryFermat, Euler and Wilson

Fermat's Little Theorem

If p is prime and p ∤ a, then aᵖ⁻¹ ≡ 1 (mod p), so exponents can be reduced mod p − 1.

Not started Jump to practice

Repeated squaring makes huge powers cheap. For a prime modulus, a theorem from 1640 makes many of them free: the powers of $a$ mod $p$ come back to 1 after at most $p - 1$ steps, so a huge exponent can be shrunk before you start.

Fermat's little theorem. If $p$ is prime and $p \nmid a$, then $$a^{p-1} \equiv 1 \pmod{p}.$$ Multiplying by $a$ gives a form that holds for every integer $a$: $a^{p} \equiv a \pmod{p}$.

Why it works

Look at the $p - 1$ numbers $a, 2a, 3a, \dots, (p-1)a$ mod $p$. None is $0$ (Euclid's lemma), and no two are equal: if $ia \equiv ja$, multiply by the inverse of $a$ to get $i \equiv j$. So they are the numbers $1, 2, \dots, p - 1$ in some new order. Multiply all of them together both ways: $$a^{p-1} \cdot (p-1)! \equiv (p-1)! \pmod{p}.$$ $(p-1)!$ is coprime to $p$, so it has an inverse and cancels, leaving $a^{p-1} \equiv 1$.

A second proof of the form $a^{p} \equiv a$ uses the binomial theorem. For $0 < k < p$, the coefficient $\binom{p}{k} = \frac{p!}{k!\,(p-k)!}$ has the prime $p$ upstairs and not downstairs, so $p \mid \binom{p}{k}$. Hence $(a + 1)^{p} \equiv a^{p} + 1 \pmod{p}$, and starting from $0^{p} = 0$ and adding 1 at a time gives $a^{p} \equiv a$ for every $a \ge 0$. Fermat stated the theorem in 1640 without proof; Euler published the first proof in 1736.

The payoff: reduce exponents mod $p - 1$. If $e = (p-1)q + r$, then $a^{e} = (a^{p-1})^{q} a^{r} \equiv a^{r} \pmod{p}$.

The powers of 3 mod 7 repeat every 6 steps, and $3^{6} \equiv 1$. So $3^{100}$ sits at the same place in the cycle as $3^{4}$, because $100 = 6 \cdot 16 + 4$. Try other bases: every row of 6 ends in 1.

Worked example: A huge exponent

Find $3^{100} \bmod 7$.
  1. $7$ is prime and $7 \nmid 3$, so $3^{6} \equiv 1 \pmod{7}$.
  2. $100 = 6 \cdot 16 + 4$, so $3^{100} \equiv 3^{4} = 81$.
  3. $81 = 7 \cdot 11 + 4$.

Answer: $3^{100} \equiv 4 \pmod{7}$.

Worked example: Another prime

Find $2^{1000} \bmod 13$.
  1. $2^{12} \equiv 1 \pmod{13}$. $1000 = 12 \cdot 83 + 4$.
  2. $2^{1000} \equiv 2^{4} = 16 \equiv 3 \pmod{13}$.

Answer: $3$.

Worked example: Inverses for free

Use Fermat to find $3^{-1} \bmod 7$.
  1. $3 \cdot 3^{5} = 3^{6} \equiv 1$, so $3^{-1} \equiv 3^{5} \pmod{7}$. In general $a^{-1} \equiv a^{p-2} \pmod{p}$.
  2. $3^{5} = 243 = 7 \cdot 34 + 5$, so $3^{-1} \equiv 5$. Check: $3 \cdot 5 = 15 \equiv 1$.

Answer: $5$.

Common mistake

Reducing the exponent mod $p$ instead of mod $p - 1$. For $3^{100} \bmod 7$, "$100 \equiv 2 \pmod{7}$, so the answer is $3^{2} = 9 \equiv 2$" is wrong; the cycle has length $p - 1 = 6$, and the answer is $4$. Also check $p \nmid a$ first: $14^{100} \equiv 0 \pmod{7}$, not $1$, because $7 \mid 14$.
Fermat's theorem fails for composite moduli in general: $2^{3} = 8 \equiv 0 \pmod{4}$, not $1$. Euler's theorem fixes this by replacing $p - 1$ with $\varphi(n)$. And the theorem's converse is false, which is the subject of pseudoprimes.

Key idea

For a prime $p$ not dividing $a$: $a^{p-1} \equiv 1 \pmod{p}$. So exponents may be reduced mod $p - 1$, and $a^{-1} \equiv a^{p-2}$.

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 (Fermat's little theorem)
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: Fermat's little theorem
@teddyrookbook GitHub