Fermat’s Little Theorem
Fermat’s little theorem says that if p is a prime and a is an integer not divisible by p, then ap−1 ≡ 1 (mod p). An equivalent form, ap ≡ a (mod p), holds for every integer a. The theorem lets you shrink a huge exponent: reduce it mod p − 1 before you compute the power.
Before this lesson: Modular Arithmetic, Linear Congruences and Chinese Remainder Theorem
What Fermat’s little theorem says
Take the prime 7 and raise each of 1, 2, …, 6 to higher and higher powers, reducing mod 7 each time. The rows look different, but every row reaches 1 at the sixth power. Fermat’s little theorem says this always happens: for a prime p, the (p − 1)th power of any number not divisible by p is 1 mod p.
a^{p-1} \equiv 1 \pmod{p} \qquad \text{if } p \text{ is prime and } p \nmid aMultiplying both sides by a gives a second form. It also covers multiples of p, since then both sides are 0 mod p.
a^{p} \equiv a \pmod{p} \qquad \text{for every integer } aPierre de Fermat stated the result in a letter in 1640. It is called “little” to tell it apart from Fermat’s last theorem, a different and much harder statement about an + bn = cn.
Why the theorem is true
Suppose p does not divide a. Look at the p − 1 numbers a, 2a, 3a, …, (p − 1)a reduced mod p. None is 0, because p divides neither a nor any number from 1 to p − 1. No two are equal: if ia ≡ ja (mod p), then p divides (i − j)a, so p divides i − j, which forces i = j.
So these p − 1 remainders are exactly 1, 2, …, p − 1 in some order. For p = 7 and a = 3, the multiples 3, 6, 9, 12, 15, 18 reduce to 3, 6, 2, 5, 1, 4. Multiplying all of them together gives the same product either way:
a \cdot 2a \cdot 3a \cdots (p-1)a \equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \pmod{p}a^{p-1}(p-1)! \equiv (p-1)! \pmod{p}Every factor of (p − 1)! is relatively prime to p, so (p − 1)! has an inverse mod p. Cancel it to get ap−1 ≡ 1 (mod p).
How to reduce a large power mod a prime
- Check that p is prime and that p does not divide a. (If p divides a, the answer is 0.)
- Reduce the base a mod p.
- Divide the exponent n by p − 1: n = q(p − 1) + r with 0 ≤ r ≤ p − 2.
- Replace an by ar, because an = (ap−1)q · ar ≡ 1q · ar.
- Compute ar mod p, squaring and reducing as you go.
a^{n} \equiv a^{\,n \bmod (p-1)} \pmod{p} \qquad (p \nmid a)Notice the two different moduli: the base is reduced mod p, but the exponent is reduced mod p − 1.
Fermat’s little theorem examples
Example 1: Checking the theorem for p = 7
Verify that 36 ≡ 1 (mod 7) by direct computation.
3^2 = 9 \equiv 2 \pmod{7}Square 3 and reduce: 9 = 7 + 2.
3^3 \equiv 2 \cdot 3 = 6 \pmod{7}Multiply by 3 once more.
3^6 = (3^3)^2 \equiv 6^2 = 36 \pmod{7}Square the cube (see exponent rules).
36 = 7 \cdot 5 + 1 \equiv 1 \pmod{7}Reduce mod 7.
Answer: 36 ≡ 1 (mod 7), as the theorem predicts with p = 7 and a = 3. Notice that 6 ≡ −1, so 33 ≡ −1 and its square is 1.
Example 2: A large power of 2
Find 2100 mod 13.
13 \nmid 2
13 is prime and does not divide 2, so 212 ≡ 1 (mod 13).
100 = 12 \cdot 8 + 4
Divide the exponent by p − 1 = 12.
2^{100} = (2^{12})^8 \cdot 2^4Split the power.
2^{100} \equiv 1^8 \cdot 2^4 = 16 \pmod{13}Replace 212 with 1.
16 = 13 + 3 \equiv 3 \pmod{13}Reduce mod 13.
Answer: 2100 ≡ 3 (mod 13).
Example 3: Reduce the base and the exponent
Find 172026 mod 11.
17 \equiv 6 \pmod{11}Reduce the base mod 11: 17 = 11 + 6.
2026 = 10 \cdot 202 + 6
Reduce the exponent mod p − 1 = 10.
17^{2026} \equiv 6^{2026} \equiv 6^6 \pmod{11}Use 610 ≡ 1 (mod 11).
6^2 = 36 \equiv 3 \pmod{11}Square and reduce: 36 = 33 + 3.
6^3 \equiv 3 \cdot 6 = 18 \equiv 7 \pmod{11}Multiply by 6 and reduce.
6^6 = (6^3)^2 \equiv 7^2 = 49 \pmod{11}Square the cube.
49 = 44 + 5 \equiv 5 \pmod{11}Reduce mod 11.
Answer: 172026 ≡ 5 (mod 11).
Example 4: A modular inverse from Fermat’s little theorem
Since a · ap−2 = ap−1 ≡ 1 (mod p), the inverse of a mod a prime p is ap−2. Use this to solve 5x ≡ 3 (mod 13).
5^{-1} \equiv 5^{11} \pmod{13}The inverse is ap−2 with p − 2 = 11.
5^2 = 25 \equiv 12 \equiv -1 \pmod{13}Square and reduce: 25 = 13 + 12.
5^4 \equiv (-1)^2 = 1 \pmod{13}Square again.
5^{11} = 5^8 \cdot 5^2 \cdot 5Split the exponent: 11 = 8 + 2 + 1.
5^{11} \equiv 1 \cdot (-1) \cdot 5 = -5 \pmod{13}Use 58 = (54)2 ≡ 1 and 52 ≡ −1.
-5 \equiv 8 \pmod{13}Add 13.
x \equiv 8 \cdot 3 = 24 \pmod{13}Multiply both sides of 5x ≡ 3 by the inverse 8.
24 \equiv 11 \pmod{13}Reduce: 24 = 13 + 11.
Answer: x ≡ 11 (mod 13). Check: 5 · 8 = 40 = 3 · 13 + 1, and 5 · 11 = 55 = 4 · 13 + 3. The extended Euclidean algorithm gives the same inverse and also works when the modulus is not prime.
Example 5: What day of the week? (word problem)
Today is Friday. What day of the week will it be 10100 days from now?
10^{100} \bmod 7The days repeat every 7, so only the remainder mod 7 matters.
10 \equiv 3 \pmod{7}Reduce the base: 10 = 7 + 3.
100 = 6 \cdot 16 + 4
7 is prime, so reduce the exponent mod 6.
10^{100} \equiv 3^{4} \pmod{7}Use 36 ≡ 1 (mod 7).
3^4 = 81 = 7 \cdot 11 + 4 \equiv 4 \pmod{7}Compute and reduce.
\text{Friday} + 4 \text{ days} = \text{Tuesday}Count Saturday, Sunday, Monday, Tuesday.
Answer: Tuesday. A googol of days is far too many to count one by one, but only its remainder mod 7 matters.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Reducing the exponent mod p instead of p − 1 | 2100 mod 13: 100 ≡ 9 (mod 13), so 29 = 512 ≡ 5 | Reduce the exponent mod 12: 100 ≡ 4, so 2100 ≡ 24 ≡ 3. |
| Reducing the base mod p − 1 | 172026 mod 11 with 17 ≡ 7 (mod 10) | The base goes mod p: 17 ≡ 6 (mod 11). |
| Using the theorem with a composite modulus | “28 ≡ 1 (mod 9)” | 9 is not prime, and 28 = 256 ≡ 4 (mod 9). Use Euler’s theorem with φ(9) = 6 instead. |
| Forgetting that p must not divide a | “146 ≡ 1 (mod 7)” | 14 ≡ 0 (mod 7), so 146 ≡ 0. Only ap ≡ a holds for multiples of p. |
| Mishandling a zero remainder | 224 mod 13: 24 = 12 · 2 + 0, so the answer is 0 | A remainder of 0 means a0 = 1, so 224 ≡ 1. |
| Using the converse | “2340 ≡ 1 (mod 341), so 341 is prime” | 341 = 11 · 31. Passing the test does not prove that a number is prime. |
Related ideas: the Fermat primality test and Euler’s theorem
Read in reverse, the theorem gives a quick test for compositeness. If an−1 ≢ 1 (mod n) for some a with gcd(a, n) = 1, then n is not prime. For example, test n = 91 with a = 2, using the Chinese remainder theorem with 91 = 7 · 13:
2^{90} = (2^3)^{30} \equiv 1 \pmod{7}23 = 8 ≡ 1 (mod 7).
90 = 12 \cdot 7 + 6
Reduce the exponent mod 12 for the prime 13.
2^{90} \equiv 2^6 = 64 \equiv 12 \pmod{13}Use 212 ≡ 1 (mod 13), then 64 = 52 + 12.
2^{90} \equiv 64 \pmod{91}64 ≡ 1 (mod 7) and 64 ≡ 12 (mod 13), so 64 solves both.
Since 290 ≡ 64, not 1, mod 91, the number 91 is composite. The test can be fooled: 341 = 11 · 31 passes with a = 2, and Carmichael numbers such as 561 = 3 · 11 · 17 pass for every a that is relatively prime to them.
For any modulus n, Euler’s theorem says aφ(n) ≡ 1 (mod n) when gcd(a, n) = 1, where φ(n) counts the integers from 1 to n that are relatively prime to n. For a prime, φ(p) = p − 1, which gives Fermat’s little theorem back. RSA Encryption uses Euler’s theorem with n = pq and φ(n) = (p − 1)(q − 1).
Practice problems
- Find 412 mod 13.
- Find 350 mod 7.
- Find 21000 mod 11.
- Find 7222 mod 5.
- Find 23100 mod 17.
- Use Fermat’s little theorem to find the inverse of 3 mod 11.
- Solve 4x ≡ 5 (mod 17) with an inverse from Fermat’s little theorem.
- Use the Fermat test with a = 2 to show that 21 is not prime.
- Show that n5 − n is divisible by 30 for every integer n.
- What day of the week will it be 2100 days after a Monday?
Show answers
1.
13 \nmid 4
13 is prime and does not divide 4.
4^{12} \equiv 1 \pmod{13}Apply the theorem with p − 1 = 12.
Answer: 1
2.
50 = 6 \cdot 8 + 2
Reduce the exponent mod 6.
3^{50} \equiv 3^2 = 9 \pmod{7}Use 36 ≡ 1 (mod 7).
9 \equiv 2 \pmod{7}Reduce: 9 = 7 + 2.
Answer: 2
3.
1000 = 10 \cdot 100 + 0
Reduce the exponent mod 10.
2^{1000} = (2^{10})^{100} \equiv 1^{100} = 1 \pmod{11}The remainder is 0, so the answer is 1.
Answer: 1
4.
7 \equiv 2 \pmod{5}Reduce the base mod 5.
222 = 4 \cdot 55 + 2
Reduce the exponent mod 4.
7^{222} \equiv 2^2 = 4 \pmod{5}Use 24 ≡ 1 (mod 5).
Answer: 4
5.
23 \equiv 6 \pmod{17}Reduce the base: 23 = 17 + 6.
100 = 16 \cdot 6 + 4
Reduce the exponent mod 16.
23^{100} \equiv 6^4 \pmod{17}Use 616 ≡ 1 (mod 17).
6^2 = 36 \equiv 2 \pmod{17}Square and reduce: 36 = 34 + 2.
6^4 \equiv 2^2 = 4 \pmod{17}Square again.
Answer: 4
6.
3^{-1} \equiv 3^{9} \pmod{11}The inverse is ap−2 with p − 2 = 9.
3^5 = 243 = 11 \cdot 22 + 1 \equiv 1 \pmod{11}Compute 35 and reduce.
3^9 = 3^5 \cdot 3^4 \equiv 1 \cdot 81 \pmod{11}Split the exponent: 9 = 5 + 4.
81 = 77 + 4 \equiv 4 \pmod{11}Reduce mod 11.
Answer: 3−1 ≡ 4 (mod 11). Check: 3 · 4 = 12 ≡ 1
7.
4^{-1} \equiv 4^{15} \pmod{17}The inverse is ap−2 with p − 2 = 15.
4^2 = 16 \equiv -1 \pmod{17}Square and reduce.
4^4 \equiv (-1)^2 = 1 \pmod{17}Square again.
4^{15} = (4^4)^3 \cdot 4^3 \equiv 64 \pmod{17}Split the exponent: 15 = 12 + 3.
64 = 51 + 13 \equiv 13 \pmod{17}Reduce mod 17.
x \equiv 13 \cdot 5 = 65 \pmod{17}Multiply both sides by the inverse 13.
65 = 51 + 14 \equiv 14 \pmod{17}Reduce mod 17.
Answer: x ≡ 14 (mod 17). Check: 4 · 14 = 56 = 3 · 17 + 5
8.
20 = 2 \cdot 10 + 0
For the prime 3, reduce the exponent mod 2.
2^{20} \equiv 2^0 = 1 \pmod{3}The remainder is 0.
20 = 6 \cdot 3 + 2
For the prime 7, reduce the exponent mod 6.
2^{20} \equiv 2^2 = 4 \pmod{7}Use 26 ≡ 1 (mod 7).
2^{20} \equiv 4 \pmod{21}4 ≡ 1 (mod 3) and 4 ≡ 4 (mod 7), so 4 solves both.
Answer: 220 ≡ 4, not 1, mod 21, so 21 is not prime
9.
n^5 \equiv n \pmod{5}Use ap ≡ a with p = 5.
n^3 \equiv n \pmod{3}Use ap ≡ a with p = 3.
n^5 = n^3 \cdot n^2 \equiv n \cdot n^2 = n^3 \pmod{3}Replace n3 with n.
n^3 \equiv n \pmod{3}Use n3 ≡ n again, so n5 ≡ n (mod 3).
n^5 \equiv n \pmod{2}n5 and n have the same parity: both odd or both even.
Answer: 2, 3 and 5 each divide n5 − n, and they are pairwise relatively prime, so 30 = 2 · 3 · 5 divides it
10.
2^{100} \bmod 7The days repeat every 7.
100 = 6 \cdot 16 + 4
7 is prime, so reduce the exponent mod 6.
2^{100} \equiv 2^4 = 16 \pmod{7}Use 26 ≡ 1 (mod 7).
16 = 14 + 2 \equiv 2 \pmod{7}Reduce mod 7.
\text{Monday} + 2 \text{ days} = \text{Wednesday}Count Tuesday, Wednesday.
Answer: Wednesday
Fermat’s little theorem FAQ
What is Fermat’s little theorem?
If p is prime and p does not divide a, then ap−1 ≡ 1 (mod p). Equivalently, ap ≡ a (mod p) for every integer a.
How do you use Fermat’s little theorem to find a remainder?
Reduce the base mod p and the exponent mod p − 1, then compute the smaller power. For example, 2100 ≡ 24 ≡ 3 (mod 13), because 100 leaves remainder 4 when divided by 12.
Does Fermat’s little theorem work for composite numbers?
Not in general: 28 ≡ 4 (mod 9), not 1. For a composite modulus n, use Euler’s theorem, aφ(n) ≡ 1 (mod n) when gcd(a, n) = 1.
What is the difference between Fermat’s little theorem and Fermat’s last theorem?
The little theorem is about powers mod a prime and has a short proof. The last theorem says an + bn = cn has no solutions in positive integers when n ≥ 3; Andrew Wiles proved it in 1995.
Summary
- If p is prime and p does not divide a, then ap−1 ≡ 1 (mod p); ap ≡ a (mod p) holds for every a.
- To find an mod p, reduce the base mod p and the exponent mod p − 1.
- A remainder of 0 in the exponent means the power is 1 mod p.
- The inverse of a mod a prime p is ap−2.
- The theorem fails for most composite moduli. Euler’s theorem replaces it there, and a number that fails the Fermat test is composite.
Previous lesson: Chinese Remainder Theorem
Next lesson: RSA Encryption
Back to the Discrete Math Index