Fermat's Little Theorem

⚡ Key Takeaways

Fermat's Little Theorem: If pp is prime and aa is not divisible by pp, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}. Equivalently, ap≡a(modp)a^p \equiv a \pmod{p} for every integer aa. Key result in words: raising any integer to the pp-th power leaves it unchanged modulo a prime pp. Where it applies: modular arithmetic, primality testing, cryptography (RSA), and the structure of the multiplicative group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times. Who/when: stated by Pierre de Fermat in 1640; first published proof by Leonhard Euler, 1736.

💡 The Core Insight

In the world of arithmetic modulo a prime pp, multiplying every nonzero residue {1,2,…,p−1}\{1, 2, \dots, p-1\} by a fixed nonzero element aa merely permutes that set. Permutations preserve products, so the product of all elements equals the product of all elements times ap−1a^{p-1}. Cancelling the (invertible) product forces ap−1≡1a^{p-1} \equiv 1. The primality of pp is exactly what guarantees that nonzero residues can be cancelled — that Z/pZ\mathbb{Z}/p\mathbb{Z} is a field.

Formal Statement

DefinitionCongruence Modulo n

For integers aa, bb and a positive integer nn, we write a≡b(modn)a \equiv b \pmod{n} ("aa is congruent to bb modulo nn") iff nn divides a−ba - b. The set of integers congruent to aa modulo nn is the residue class [a]n[a]_n. Modular arithmetic is well-defined: if a≡a′a \equiv a' and b≡b′b \equiv b', then a+b≡a′+b′a+b \equiv a'+b' and ab≡a′b′ab \equiv a'b'.

The theorem has two equivalent standard forms.

1
ap−1≡1(modp),gcd⁡(a,p)=1.\quad a^{p-1} \equiv 1 \pmod{p}, \qquad \gcd(a,p)=1.

Derivation hint: this is the "coprime" form; it requires p∤ap \nmid a so that aa is invertible modulo pp.

2
ap≡a(modp),for all a∈Z.\quad a^{p} \equiv a \pmod{p}, \qquad \text{for all } a \in \mathbb{Z}.

Derivation hint: multiply [1] by aa; this version holds even when p∣ap \mid a since then both sides are ≡0\equiv 0. Here the symbols are: pp a prime number; aa an arbitrary integer; gcd⁡(a,p)\gcd(a,p) the greatest common divisor; and ≡(modp)\equiv \pmod p denotes congruence modulo pp.

TheoremFermat's Little Theorem

Let pp be a prime. Then for every integer aa with p∤ap \nmid a, equation [1] holds. Equivalently, for every integer aa whatsoever, equation [2] holds. Moreover, the order ordp(a)\mathrm{ord}_p(a) of aa modulo pp — the least positive kk with ak≡1a^k \equiv 1 — divides p−1p-1.

3
ordp(a)∣(p−1).\quad \mathrm{ord}_p(a) \mid (p-1).

Derivation hint: by the division algorithm write p−1=q⋅ordp(a)+rp-1 = q\cdot\mathrm{ord}_p(a) + r with 0≤r<ordp(a)0 \le r < \mathrm{ord}_p(a); then ar≡1a^r \equiv 1 forces r=0r = 0 by minimality of the order.

TheoremEuler's Generalization

For any modulus n≥1n \ge 1 and any integer aa coprime to nn,

4
aφ(n)≡1(modn),\quad a^{\varphi(n)} \equiv 1 \pmod{n},

where φ\varphi is Euler's totient function, φ(n)=∣{1≤k≤n:gcd⁡(k,n)=1}∣\varphi(n) = |\{1 \le k \le n : \gcd(k,n)=1\}|. FLT is the special case n=pn = p prime, since φ(p)=p−1\varphi(p) = p-1. Derivation hint: replace the full residue system {1,…,p−1}\{1,\dots,p-1\} in the proof below by the reduced residue system of units modulo nn, which has size φ(n)\varphi(n).

Why It Holds / Derivation

ProofProof via Rearrangement of Residues

We prove form [1]. Fix a prime pp and an integer aa with p∤ap \nmid a.

Step 1. Consider the set of nonzero residues S={1,2,…,p−1}S = \{1, 2, \dots, p-1\}. Form the products a⋅1, a⋅2, …, a⋅(p−1)a\cdot 1,\, a\cdot 2,\, \dots,\, a\cdot(p-1) and reduce each modulo pp.

Step 2. No product is ≡0\equiv 0. Since pp is prime and p∤ap \nmid a and p∤kp \nmid k for 1≤k≤p−11 \le k \le p-1, we have p∤akp \nmid ak (Euclid's lemma). So each ak  pak \bmod p lies in SS.

Step 3. The products are distinct. If ai≡aj(modp)ai \equiv aj \pmod p, then p∣a(i−j)p \mid a(i-j); since p∤ap \nmid a, p∣(i−j)p \mid (i-j), forcing i=ji = j for i,j∈Si,j \in S. Hence the map k↦ak  pk \mapsto ak \bmod p is injective on a finite set, thus a bijection of SS onto itself — a permutation.

Step 4. Because multiplication is commutative, the product of all the akak equals the product of all elements of SS:

5
∏k=1p−1(ak)≡∏k=1p−1k(modp).\quad \prod_{k=1}^{p-1} (a k) \equiv \prod_{k=1}^{p-1} k \pmod{p}.

Derivation hint: Step 3 says the left product runs over the same set of residues, merely reordered.

Step 5. Factor out aa from each of the p−1p-1 terms on the left:

6
ap−1⋅(p−1)!≡(p−1)!(modp).\quad a^{p-1} \cdot (p-1)! \equiv (p-1)! \pmod{p}.

Derivation hint: ∏k=1p−1(ak)=ap−1∏k=1p−1k=ap−1(p−1)!\prod_{k=1}^{p-1}(ak) = a^{p-1}\prod_{k=1}^{p-1}k = a^{p-1}(p-1)!.

Step 6. Since pp is prime, gcd⁡((p−1)!,p)=1\gcd((p-1)!, p) = 1 — no factor of (p−1)!(p-1)! is divisible by pp. Therefore (p−1)!(p-1)! is invertible modulo pp and may be cancelled, yielding

7
ap−1≡1(modp).■\quad a^{p-1} \equiv 1 \pmod{p}. \qquad \blacksquare

Derivation hint: cancel the unit (p−1)!(p-1)! from both sides of [6]; cancellation is legitimate precisely because Z/pZ\mathbb{Z}/p\mathbb{Z} is a field.

∎

:::proof[Alternative Proof via Induction (form [2])] We show ap≡a(modp)a^p \equiv a \pmod p for all integers a≥0a \ge 0 by induction; negative aa and the coprime form follow.

Base: 0p=0≡00^p = 0 \equiv 0.

Inductive step: assume ap≡aa^p \equiv a. By the binomial theorem,

8
(a+1)p=∑k=0p(pk)ak=ap+1+∑k=1p−1(pk)ak.\quad (a+1)^p = \sum_{k=0}^{p} \binom{p}{k} a^k = a^p + 1 + \sum_{k=1}^{p-1}\binom{p}{k}a^k.

Derivation hint: split off the k=0k=0 and k=pk=p terms (11 and apa^p) from the binomial expansion. For 1≤k≤p−11 \le k \le p-1, the binomial coefficient (pk)=p!k!(p−k)!\binom{p}{k} = \frac{p!}{k!(p-k)!} is divisible by pp, because pp divides the numerator p!p! but not the denominator k!(p−k)!k!(p-k)! (all factors are <p< p, and pp is prime). Hence the sum vanishes modulo pp, giving (a+1)p≡ap+1≡a+1(modp)(a+1)^p \equiv a^p + 1 \equiv a + 1 \pmod p, completing the induction. ■\blacksquare

mathematician · Pierre de Fermat | 1607–1665

A magistrate at the parlement of Toulouse, Fermat pursued mathematics as an avocation yet helped found number theory, analytic geometry, probability, and the calculus of maxima and minima. He communicated results in letters — typically without proofs. In a letter of 18 October 1640 to Bernard Frénicle de Bessy he stated what we now call his Little Theorem, remarking he would send the proof "if it were not so long." That proof, if it existed, was never recorded; Euler supplied the first published demonstration nearly a century later. Fermat's habit of stating theorems without proof also produced his far harder Last Theorem (xn+yn=znx^n + y^n = z^n has no positive integer solutions for n>2n>2), resolved only by Wiles in 1994.

Building Intuition

🧠 Intuition

Think of the nonzero residues mod pp as beads on a circular bracelet. "Multiply everything by aa" is a shuffle that picks up each bead and drops it in another slot — but it never collapses two beads together and never sends one to the empty (zero) slot. Because it's a clean reshuffle (a permutation), the collection of beads is unchanged; only the labels moved. The total product of bead-labels is therefore the same before and after, and the only difference between "before" and "after" is the factor of aa that got multiplied in p−1p-1 times. So that accumulated factor ap−1a^{p-1} must be invisible — i.e., ≡1\equiv 1. The primality of pp is what forbids two beads from merging: in Z/6Z\mathbb{Z}/6\mathbb{Z}, multiplying by 22 sends both 33 and 00 toward collisions because 66 is composite, and the bracelet trick fails.

Figure
Multiplication by a=3a=3 permutes the nonzero residues modulo p=7p=7, the geometric heart of the proof.
×3 mod 7 is a permutation of {1,...,6} 1 2 3 4 5 6 cycle 1→3→2→6→4→5→1: every residue hit exactly once

Worked Examples

ExampleReducing a Huge Power

Given: Compute 21000000  72^{1000000} \bmod 7.

Step 1. 77 is prime and 7∤27 \nmid 2, so FLT gives 26≡1(mod7)2^{6} \equiv 1 \pmod 7. (Check: 26=64=9⋅7+12^6 = 64 = 9\cdot 7 + 1.)

Step 2. Reduce the exponent modulo p−1=6p-1 = 6: 1000000=6⋅166666+41000000 = 6 \cdot 166666 + 4, so 1000000≡4(mod6)1000000 \equiv 4 \pmod 6.

Step 3. Therefore

9
21000000=(26)166666⋅24≡1166666⋅24≡16(mod7).\quad 2^{1000000} = \big(2^{6}\big)^{166666}\cdot 2^{4} \equiv 1^{166666}\cdot 2^4 \equiv 16 \pmod 7.

Derivation hint: split the exponent using 26≡12^6 \equiv 1, then evaluate the residual 242^4.

Step 4. 16=2⋅7+216 = 2\cdot 7 + 2, so 16≡216 \equiv 2.

21000000≡2(mod7)\boxed{2^{1000000} \equiv 2 \pmod 7}
ExampleModular Inverse via FLT

Given: Find the multiplicative inverse of a=5a = 5 modulo p=13p = 13.

Step 1. Since 1313 is prime and 13∤513 \nmid 5, FLT gives 512≡15^{12}\equiv 1, hence 5⋅511≡15\cdot 5^{11}\equiv 1. So the inverse is 511  135^{11}\bmod 13.

10
a−1≡ap−2(modp).\quad a^{-1} \equiv a^{p-2} \pmod{p}.

Derivation hint: a⋅ap−2=ap−1≡1a\cdot a^{p-2} = a^{p-1}\equiv 1, so ap−2a^{p-2} is the inverse.

Step 2. Compute 511  135^{11}\bmod 13 by repeated squaring: 52=25≡12≡−15^2 = 25 \equiv 12 \equiv -1. Then 54≡(−1)2=15^4 \equiv (-1)^2 = 1, 58≡15^8 \equiv 1.

Step 3. 511=58⋅52⋅51≡1⋅(−1)⋅5=−5≡8(mod13)5^{11} = 5^{8}\cdot 5^{2}\cdot 5^{1} \equiv 1\cdot(-1)\cdot 5 = -5 \equiv 8 \pmod{13}.

Step 4. Verify: 5⋅8=40=3⋅13+1≡15\cdot 8 = 40 = 3\cdot 13 + 1 \equiv 1. ✓

5−1≡8(mod13)\boxed{5^{-1} \equiv 8 \pmod{13}}

Common Misconceptions

⚠️ The Converse Is False

The implication runs prime ⟹ congruence, not the reverse. From "an−1≡1(modn)a^{n-1}\equiv 1 \pmod n for some particular aa" you cannot conclude that nn is prime. Composite numbers can satisfy the Fermat congruence for many bases; those that satisfy it for one base are Fermat pseudoprimes to that base, and those satisfying it for all coprime bases are Carmichael numbers. Also note the coprimality requirement in form [1]: if p∣ap \mid a then ap−1≡0a^{p-1}\equiv 0, not 11 — use form [2] instead, which is unconditional.

CounterexampleCarmichael Number 561

Take n=561=3⋅11⋅17n = 561 = 3\cdot 11\cdot 17, which is composite. Yet for every integer aa coprime to 561561,

11
a560≡1(mod561).\quad a^{560} \equiv 1 \pmod{561}.

Derivation hint: 560560 is divisible by 2,10,162,10,16 — the values of p−1p-1 for p=3,11,17p=3,11,17 — so FLT holds modulo each prime factor, and the Chinese Remainder Theorem combines them. A naive "Fermat test" with random bases would repeatedly declare 561561 "probably prime." Carmichael numbers (561,1105,1729,…561, 1105, 1729, \dots) are infinite in number (Alford–Granville–Pomerance, 1994), which is why the strong (Miller–Rabin) test, not the plain Fermat test, is used in practice.

Connections

connection

FLT is the prime case of Euler's Theorem via the Euler Totient Function, and both are corollaries of Lagrange's Theorem (Group Theory) applied to the unit group (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times: an element's order divides the group order. The structure theorem that (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic sharpens FLT — it guarantees a Primitive Root of order exactly p−1p-1. The induction proof exposes the Freshman's Dream identity (a+b)p≡ap+bp(modp)(a+b)^p \equiv a^p + b^p \pmod p, a hallmark of the Frobenius Endomorphism in fields of characteristic pp. FLT also underpins Wilson's Theorem (via (p−1)!≡−1(p-1)! \equiv -1), the Chinese Remainder Theorem when gluing congruences, and the theory of Quadratic Residues through Euler's criterion a(p−1)/2≡(ap)a^{(p-1)/2}\equiv \left(\tfrac{a}{p}\right).

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1640 — Pierre de Fermat states the theorem in a letter to Frénicle de Bessy, motivated by his study of perfect numbers and Mersenne primes; he omits the proof.
- 1736 — Leonhard Euler publishes the first proof, Theorematum quorundam ad numeros primos spectantium demonstratio, using a binomial/induction argument.
- 1750s–1763 — Euler generalizes to composite moduli, introducing the totient φ(n)\varphi(n) and proving Euler's theorem [4].
- 1770–1771 — Lagrange and Wilson context: Lagrange proves Wilson's theorem, tightly connected to FLT and (p−1)!(p-1)!.
- 1801 — Gauss, in Disquisitiones Arithmeticae, systematizes congruence notation ≡\equiv and recasts FLT within the algebra of residue classes.
- 1801 — Gauss proves the existence of primitive roots, showing (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic, refining FLT's order statement [3].
- 1910 — Robert Carmichael identifies the absolute pseudoprimes (Carmichael numbers), exposing the failure of the Fermat test's converse.
- 1976–1980 — Miller and Rabin develop the strong probabilistic primality test grounded in Fermat-type identities.
- 1994 — Alford, Granville, and Pomerance prove there are infinitely many Carmichael numbers.

Subtleties & Limits

open-problem

While FLT itself is settled, the boundary questions it spawns remain rich and partly open. Fermat quotients qp(a)=ap−1−1p  pq_p(a) = \frac{a^{p-1}-1}{p} \bmod p encode finer information; primes with qp(2)≡0q_p(2)\equiv 0 are Wieferich primes, and only two (10931093 and 35113511) are known — whether infinitely many exist is open, and surprisingly, this connects to the first case of Fermat's Last Theorem. The distribution of bases for which a composite nn passes the Fermat test, the density of Carmichael numbers, and the exact strength of the Artin conjecture (that a given non-square integer is a primitive root for infinitely many primes — unproven unconditionally) all probe how sharply FLT's converse fails. Algorithmically, whether deterministic polynomial-time primality (achieved by AKS, 2002) can be made as fast as the Fermat-based probabilistic tests in practice remains a live engineering frontier.

See Also

Euler's Theorem · Euler Totient Function · Lagrange's Theorem (Group Theory) · Carmichael Number · Primitive Root · Wilson's Theorem · Chinese Remainder Theorem · Quadratic Residues · RSA Cryptosystem · Frobenius Endomorphism