Fermat's Little Theorem and Wilson's Theorem
Fermat's little theorem and Wilson's theorem
Fermat's little theorem
Theorem. Let p be a prime.
- If p does not divide a, then ap−1 ≡ 1 (mod p).
- For every integer a, ap ≡ a (mod p).
Both hypotheses matter. The modulus must be prime, and form 1 needs gcd(a, p) = 1: if p divides a, then ap−1 ≡ 0, not 1. Form 2 covers that case, since both sides are then 0.
Why it is true (sketch). If p does not divide a, multiplying the nonzero residues 1, 2, …, p − 1 by a only rearranges them modulo p.
Taking the product of both lists gives ap−1(p − 1)! ≡ (p − 1)! (mod p), and since (p − 1)! is not divisible by p, it can be cancelled, leaving ap−1 ≡ 1.
Reducing large powers
If gcd(a, p) = 1, write the exponent as n = q(p − 1) + r with 0 ≤ r < p − 1. Then an = (ap−1)qar ≡ ar (mod p). In short: reduce the exponent modulo p − 1, not modulo p.
Example. Find 3100 mod 7. Since 7 is prime and does not divide 3, 36 ≡ 1. 100 = 6 × 16 + 4, so 3100 ≡ 34 = 81 = 77 + 4 ≡ 4 (mod 7).
Towers. If the exponent is itself a power, reduce it modulo p − 1 first (using whatever periodicity it has modulo p − 1), then finish modulo p. For example, 234 mod 5: 34 = 81 ≡ 1 (mod 4), so 281 ≡ 21 = 2 (mod 5).
Inverses from Fermat
If p is prime and p does not divide a, then a · ap−2 = ap−1 ≡ 1, so ap−2 is the inverse of a modulo p. For small moduli it is often quicker to spot the inverse directly, for example 2 · 4 = 8 ≡ 1 (mod 7), so 2−1 ≡ 4 (mod 7).
The Fermat primality test and its limits
Fermat's theorem gives a compositeness test. If, for some a with gcd(a, n) = 1, an−1 ≢ 1 (mod n), then n is certainly composite; a is called a Fermat witness. For example, 214 = 16,384 = 15 × 1,092 + 4, so 214 ≡ 4 (mod 15), proving 15 composite without factoring it.
The converse is false. A composite n can satisfy an−1 ≡ 1 (mod n) for a particular base; it is then a Fermat pseudoprime to that base.
Worse, Carmichael numbers such as 561 = 3 × 11 × 17 satisfy the congruence for every base coprime to them. Passing the test is evidence of primality, not proof. Practical tests such as Miller–Rabin strengthen the idea.
Wilson's theorem
Theorem. For an integer n > 1, (n − 1)! ≡ −1 (mod n) if and only if n is prime.
- Why it holds for primes (sketch). Modulo a prime p, each of 1, …, p − 1 has a unique inverse in the same set. Only 1 and p − 1 are their own inverses. All other factors pair off to products ≡ 1, leaving (p − 1)! ≡ 1 · (p − 1) ≡ −1.
- Why it fails for composites. If n = ab with 1 < a < n, then a divides (n − 1)!, so (n − 1)! cannot be ≡ −1 modulo a, hence not modulo n. For instance 5! = 120 ≡ 0 (mod 6).
- Check: 6! = 720 = 7 × 102 + 6, so 6! ≡ 6 ≡ −1 (mod 7).
Wilson's theorem is a perfect characterization of primes but a hopeless practical test, because computing (n − 1)! mod n takes far too long for large n.
Using Wilson's theorem on shorter factorials
To find k! modulo a prime p with k < p − 1, write (p − 1)! = (p − 1)(p − 2)…(k + 1) · k! and replace each of the top factors by a small negative residue: p − j ≡ −j.
Example. Find 4! mod 7. 6! = 6 · 5 · 4! ≡ (−1)(−2) · 4! = 2 · 4! (mod 7). Wilson gives 2 · 4! ≡ −1, so 4! ≡ −1 · 2−1 ≡ −4 ≡ 3 (mod 7). Check: 4! = 24 = 21 + 3.
Mark Yourself Question
- Grab a piece of paper and try this problem yourself.
- When you're done, check the "I have answered this question" box below.
- View the solution and report whether you got it right or wrong.
Compute each remainder. Each modulus is prime.
- 7222 mod 11
- 31000 mod 13
- 3925 mod 13
Find the remainder when 15! is divided by 17.
Give your answer in remainder.