Fermat's Little Theorem With Proof and Uses
Understand why a to the power p minus 1 leaves remainder 1 modulo a prime, with two proofs, worked uses and a warning about the converse.
Key takeaways
- If p is prime and p does not divide a, then a^(p−1) ≡ 1 (mod p).
- For every integer a, including multiples of p, a^p ≡ a (mod p).
- The standard proof shows that multiplying by a only shuffles the nonzero remainders.
- Exponents can be reduced modulo p − 1 when the base is not a multiple of p.
- The converse is false: some composite numbers satisfy the congruence for particular bases.
Fermat's little theorem says that if p is a prime and a is a whole number not divisible by p, then a raised to the power p − 1 leaves remainder 1 when divided by p. For example, 3⁶ = 729, and 729 is one more than 104 × 7.
It lets you shrink enormous powers to tiny ones, find inverses without guesswork and prove that certain numbers are composite. It is one of the most used results in elementary number theory.
Seeing the pattern
Take p = 7 and list the powers of a few bases, keeping remainders only.
| a | a¹ | a² | a³ | a⁴ | a⁵ | a⁶ |
|---|---|---|---|---|---|---|
| 2 | 2 | 4 | 1 | 2 | 4 | 1 |
| 3 | 3 | 2 | 6 | 4 | 5 | 1 |
| 5 | 5 | 4 | 6 | 2 | 3 | 1 |
| 6 | 6 | 1 | 6 | 1 | 6 | 1 |
Every row ends in 1 at the sixth power. Some rows reach 1 sooner, after 2 or 3 steps, but always after a number of steps that divides 6.
The notation ≡ (mod p) means "leaves the same remainder on division by p". If it is new, this introduction will help.
Proof by shuffling
Let p be prime and a not divisible by p. Look at the numbers a, 2a, 3a, ..., (p − 1)a, reduced modulo p. Take p = 7 and a = 3.
multiples: 3 6 9 12 15 18
remainders: 3 6 2 5 1 4 The remainders are 1 to 6 in a new order. This always happens, for two reasons.
First, none of them is 0, because a prime that divides a product ka must divide k or a, and it divides neither. Second, no two are equal: if ia ≡ ja, then p divides (i − j)a, so p divides i − j, which forces i = j when both lie between 1 and p − 1.
So we have p − 1 different nonzero remainders, which must be all of them. Multiply the whole list together in both forms.
a × 2a × 3a × ... × (p−1)a ≡ 1 × 2 × 3 × ... × (p−1)
a^(p−1) × (p−1)! ≡ (p−1)! The factorial (p − 1)! is a product of numbers not divisible by p, so p does not divide it. It can be cancelled, leaving a^(p−1) ≡ 1. That completes the proof.
Proof by induction
A second proof establishes a^p ≡ a for a = 0, 1, 2 and so on. It holds for a = 0. Now expand (a + 1)^p by the binomial theorem. The middle coefficients, p!/(k!(p − k)!) for 0 < k < p, are all divisible by p, because the p in the numerator cannot be cancelled by anything below.
(a + 1)^p ≡ a^p + 1 (middle terms vanish mod p)
≡ a + 1 (by the assumption a^p ≡ a) You can watch the vanishing coefficients in Pascal's triangle: row 7 reads 1, 7, 21, 35, 35, 21, 7, 1. For more on this style of argument, see mathematical induction.
A proof you can wear
Make strings of p beads in a colors. There are a^p strings. Remove the a strings of a single color. Join each remaining string into a necklace and rotate it. Because p is prime, the p rotations of a mixed string are all different. So the mixed strings fall into groups of exactly p, which shows p divides a^p − a.
With p = 3 and two colors, the 8 strings are the 2 plain ones plus 6 mixed ones, forming 2 rotation groups of 3.
Use 1: taming large powers
Find the remainder of 2¹⁰⁰ on division by 13. The theorem gives 2¹² ≡ 1.
100 = 8 × 12 + 4
2¹⁰⁰ = (2¹²)⁸ × 2⁴ ≡ 1 × 16 ≡ 3 (mod 13) In general, when p does not divide a, you may replace the exponent by its remainder on division by p − 1. For composite moduli a more general tool is needed, Euler's theorem. Related tricks appear in finding the last digits of huge powers.
Use 2: finding inverses
Since a × a^(p−2) = a^(p−1) ≡ 1, the number a^(p−2) is an inverse of a modulo p.
inverse of 5 modulo 11 is 5⁹
5² ≡ 3, 5⁴ ≡ 9, 5⁸ ≡ 81 ≡ 4
5⁹ ≡ 4 × 5 = 20 ≡ 9
check: 5 × 9 = 45 ≡ 1 For large primes, repeated squaring makes this fast. The alternative is the Euclidean method used for linear congruences.
Use 3: proving a number composite
If you find an a, coprime to n, with a^(n−1) not congruent to 1 modulo n, then n is not prime. You have proved it without finding a factor.
The reverse inference fails. Consider 341 = 11 × 31. Since 2¹⁰ = 1024 = 3 × 341 + 1, we get 2³⁴⁰ = (2¹⁰)³⁴ ≡ 1. So 341 passes for base 2 although it is composite. Base 3 exposes it.
Worse, 561 passes for every base coprime to it. Numbers like that are called Carmichael numbers. The repair is the Miller-Rabin test.
The order of an element
The table of powers of 7's remainders hints at something sharper. The order of a modulo p is the smallest positive k with a^k ≡ 1. It always divides p − 1.
To see why, divide p − 1 by k: p − 1 = qk + r with r smaller than k. Then a^r ≡ 1, and since k was the smallest positive exponent, r must be 0.
Modulo 7, the order of 2 is 3 and the order of 3 is 6. A base with the largest possible order, p − 1, is called a primitive root.
Use 4: repeating decimals
The theorem explains a pattern from school arithmetic. For a prime p other than 2 and 5, the decimal expansion of 1/p repeats, and the length of the repeating block is the order of 10 modulo p. So the length always divides p − 1.
1/7 = 0.142857 142857 ... period 6, which divides 6
1/11 = 0.09 09 ... period 2, which divides 10
1/13 = 0.076923 076923 ... period 6, which divides 12
1/37 = 0.027 027 ... period 3, which divides 36 The reason is long division. Each step multiplies the current remainder by 10 and reduces modulo p. The digits start to repeat when the remainder returns to 1, and that happens first at the order of 10.
Use 5: ruling out solutions
Because there are so few possible values for certain powers, the theorem can show that an equation has no whole-number solution. Modulo 5, every fourth power is 0 or 1. So a sum of two fourth powers can only be 0, 1 or 2 modulo 5. The number 2,023 leaves remainder 3, and therefore it is not a sum of two fourth powers. No search was needed.
The same fact shows that n⁵ − n is always divisible by 5. It is also divisible by 2 and by 3, since n⁵ − n = n(n − 1)(n + 1)(n² + 1) contains three consecutive integers. Hence n⁵ − n is a multiple of 30 for every integer n.
A larger worked example
Combine the ideas to find the remainder of 5²⁰²⁶ on division by 23. Here p − 1 = 22.
2026 = 92 × 22 + 2
5²⁰²⁶ ≡ 5² = 25 ≡ 2 (mod 23) The whole calculation is a division and one squaring. Written out in full, 5²⁰²⁶ has well over a thousand digits.
Now suppose the exponent is itself a tower, such as 3 raised to 3¹⁰, taken modulo 11. You need the exponent 3¹⁰ modulo 10. Since the powers of 3 modulo 10 cycle 3, 9, 7, 1 with length 4, and 10 leaves remainder 2 on division by 4, the exponent is 9 modulo 10. Then 3⁹ modulo 11 is the inverse of 3, which is 4.
3¹⁰ ≡ 9 (mod 10)
3⁹ = 19683 ≡ 4 (mod 11) check: 3 × 4 = 12 ≡ 1 Each level of the tower is reduced by a different modulus. Mixing them up is an easy way to go wrong, so label every line.
Where it came from
Pierre de Fermat stated the result in a letter of 1640 to Bernard Frénicle de Bessy, without including a proof. Leibniz found a proof that stayed unpublished, and Euler published one in 1736. The word "little" separates it from Fermat's Last Theorem, a far harder statement about sums of powers.
In today's language it is a special case of a fact about finite groups: the order of any element divides the size of the group. The nonzero remainders modulo p form a group of size p − 1 under multiplication.
Common mistakes
Forgetting the condition on a. If p divides a, then a^(p−1) ≡ 0, not 1. The form a^p ≡ a is safe in all cases.
Reducing the exponent modulo p. Exponents work modulo p − 1, while bases work modulo p. For 10²⁵ modulo 7, reduce the base to 3 and the exponent 25 to 1, giving 3.
Using it for composite moduli. Modulo 8, 3⁷ ≡ 3, not 1.
Assuming p − 1 is the first time you hit 1. It may happen earlier, as with 2³ ≡ 1 modulo 7.
Try it yourself
- Verify the theorem for p = 11 and a = 2 by listing the ten powers. You should reach 1 for the first time at the tenth step.
- Find the remainder of 7⁸³ modulo 17. Reduce 83 modulo 16 to get 3, then compute 343 − 20 × 17 = 3.
- Find the inverse of 4 modulo 13 as 4¹¹. Check your answer, which should be 10.
- Explain why no integer n makes n² + 1 divisible by 7. Test the seven possible remainders of n and see that n² is never 6.
That last exercise points toward a larger theory: which remainders are squares modulo a prime. It begins with Euler's observation that a^((p−1)/2) is always 1 or −1, a direct consequence of Fermat's theorem, since its square is 1.
Choosing the right form
Use a^(p−1) ≡ 1 when you know the base is not a multiple of p and you want to shorten an exponent. Use a^p ≡ a when you cannot be sure, for example inside a general identity that must hold for every integer n. The two forms differ only by one factor of a, but that factor decides whether the statement survives the case of a remainder of 0.
Why it matters for cryptography
Public-key methods depend on the fact that powers modulo a number eventually cycle in a predictable way. Fermat's theorem is the prime case, and its extension underlies RSA. Small examples are for understanding only; secure systems need large numbers, careful padding and reviewed software.