Fermat's Little Theorem

Fermat's Little Theorem: If is prime and is not divisible by , then . Equivalently, for every integer . Key result in words: raising any integer to the -th power leaves it unchanged modulo a prime . Where it applies: modular arithmetic, primality testing, cryptography (RSA), and the structure of the multiplicative group . Who/when: stated by Pierre de Fermat in 1640; first published proof by Leonhard Euler, 1736.

Fermat's Little Theorem (FLT — not to be confused with Fermat's Last Theorem) is one of the foundational results of elementary number theory. Precisely, it asserts that for a prime and any integer coprime to , the residue equals modulo . This matters because it gives an immediate, computable constraint on the powers of any nonzero residue class, revealing that the multiplicative group modulo a prime has a rigid cyclic structure where every element's order divides . It is the engine behind fast modular exponentiation tricks, the Fermat primality test, and the correctness of RSA encryption. The surprising fact: despite involving exponentials, the theorem reduces a seemingly intractable computation like to a trivial one, and it holds with no exceptions whatsoever — provided is genuinely prime.

The Core Insight

In the world of arithmetic modulo a prime , multiplying every nonzero residue by a fixed nonzero element merely permutes that set. Permutations preserve products, so the product of all elements equals the product of all elements times . Cancelling the (invertible) product forces . The primality of is exactly what guarantees that nonzero residues can be cancelled — that is a field.

Formal Statement

Congruence Modulo n

For integers , and a positive integer , we write (" is congruent to modulo ") iff divides . The set of integers congruent to modulo is the residue class . Modular arithmetic is well-defined: if and , then and .

The theorem has two equivalent standard forms.

1
⟳this is the "coprime" form; it requires so that is invertible modulo .
2
⟳multiply [1] by ; this version holds even when since then both sides are .

Here the symbols are: a prime number; an arbitrary integer; the greatest common divisor; and denotes congruence modulo .

Fermat's Little Theorem

Let be a prime. Then for every integer with , equation [1] holds. Equivalently, for every integer whatsoever, equation [2] holds. Moreover, the order of modulo — the least positive with — divides .

3
⟳by the division algorithm write with ; then forces by minimality of the order.

Euler's Generalization

For any modulus and any integer coprime to ,

4
where is Euler's totient function, . FLT is the special case prime, since .
⟳replace the full residue system in the proof below by the reduced residue system of units modulo , which has size .

Why It Holds / Derivation

Proof via Rearrangement of Residues

We prove form [1]. Fix a prime and an integer with .

Step 1. Consider the set of nonzero residues . Form the products and reduce each modulo . Step 2. No product is . Since is prime and and for , we have (Euclid's lemma). So each lies in . Step 3. The products are distinct. If , then ; since , , forcing for . Hence the map is injective on a finite set, thus a bijection of onto itself — a permutation. Step 4. Because multiplication is commutative, the product of all the equals the product of all elements of :
5
⟳Step 3 says the left product runs over the same set of residues, merely reordered.
Step 5. Factor out from each of the terms on the left:
6
⟳.
Step 6. Since is prime, — no factor of is divisible by . Therefore is invertible modulo and may be cancelled, yielding
7
⟳cancel the unit from both sides of [6]; cancellation is legitimate precisely because is a field.

:::proof[Alternative Proof via Induction (form [2])] We show for all integers by induction; negative and the coprime form follow.

Base: . Inductive step: assume . By the binomial theorem,
8
⟳split off the and terms ( and ) from the binomial expansion.

For , the binomial coefficient is divisible by , because divides the numerator but not the denominator (all factors are , and is prime). Hence the sum vanishes modulo , giving , completing the induction.

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 ( has no positive integer solutions for ), resolved only by Wiles in 1994.

Building Intuition

Think of the nonzero residues mod as beads on a circular bracelet. "Multiply everything by " 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 that got multiplied in times. So that accumulated factor must be invisible — i.e., . The primality of is what forbids two beads from merging: in , multiplying by sends both and toward collisions because is composite, and the bracelet trick fails.

Multiplication by $a=3$ permutes the nonzero residues modulo $p=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

Reducing a Huge Power

Given: Compute . Step 1. is prime and , so FLT gives . (Check: .) Step 2. Reduce the exponent modulo : , so . Step 3. Therefore
9
⟳split the exponent using , then evaluate the residual .
Step 4. , so .

Modular Inverse via FLT

Given: Find the multiplicative inverse of modulo . Step 1. Since is prime and , FLT gives , hence . So the inverse is .
10
⟳, so is the inverse.
Step 2. Compute by repeated squaring: . Then , . Step 3. . Step 4. Verify: . ✓

Common Misconceptions

The Converse Is False

The implication runs prime ⟹ congruence, not the reverse. From " for some particular " you cannot conclude that 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 then , not — use form [2] instead, which is unconditional.

Carmichael Number 561

Take , which is composite. Yet for every integer coprime to ,

11
⟳ is divisible by — the values of for — so FLT holds modulo each prime factor, and the Chinese Remainder Theorem combines them.

A naive "Fermat test" with random bases would repeatedly declare "probably prime." Carmichael numbers () 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

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 : an element's order divides the group order. The structure theorem that is cyclic sharpens FLT — it guarantees a Primitive Root of order exactly . The induction proof exposes the Freshman's Dream identity , a hallmark of the Frobenius Endomorphism in fields of characteristic . FLT also underpins Wilson's Theorem (via ), the Chinese Remainder Theorem when gluing congruences, and the theory of Quadratic Residues through Euler's criterion .

Real-World Applications

  1. RSA cryptosystem (Rivest–Shamir–Adleman, 1977). Decryption correctness follows directly from FLT/Euler's theorem applied to , with .
  2. Fermat primality test. A fast probabilistic check: if for some , then is definitely composite.
  3. Miller–Rabin primality test. The strong refinement that powers modern cryptographic key generation (e.g., OpenSSL, GnuPG) rests on Fermat-type congruences plus square-root structure.
  4. Modular exponentiation and inverses. Computing (as in Example 2) is used in elliptic-curve and finite-field arithmetic, including ECDSA signatures.
  5. Pseudorandom number generators. Lehmer/multiplicative congruential generators choose multipliers that are primitive roots, whose maximal period is governed by FLT and equation [3].
  6. Error-correcting codes. Reed–Solomon and BCH codes operate over finite fields and , where every nonzero element satisfies , the field analogue of FLT.
  7. Hashing and checksums. Rolling polynomial hashes modulo a prime exploit FLT to invert and roll the hash window in .

Historical Development

  • 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 and proving Euler's theorem [4].
  • 1770–1771 — Lagrange and Wilson context: Lagrange proves Wilson's theorem, tightly connected to FLT and .
  • 1801 — Gauss, in Disquisitiones Arithmeticae, systematizes congruence notation and recasts FLT within the algebra of residue classes.
  • 1801 — Gauss proves the existence of primitive roots, showing 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

While FLT itself is settled, the boundary questions it spawns remain rich and partly open. Fermat quotients encode finer information; primes with are Wieferich primes, and only two ( and ) 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 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

Before this

Where this leads