Fermat's Little Theorem
Take any prime and any integer not divisible by . Raise to the power and reduce modulo . Astonishingly, the answer is always . Try , : the powers reduce mod to . Indeed . This is not a coincidence of small numbers. It is a theorem, discovered by Pierre de Fermat in the 1630s. It holds for every prime and every base coprime to it. The result matters because it is the first deep structural fact about how integers behave under a prime modulus. This goes beyond the bare mechanics of Congruences and Modular Arithmetic. It reveals that the nonzero residues mod form a finite algebraic system. In this system, every element has finite multiplicative order dividing . This is a fact with no visible analogue before you fix a modulus. It converts a question about a huge power, impossible to compute directly, into a small, exact statement about remainders. The big picture is that Fermat's Little Theorem is the prototype for a whole family of results. These results describe cyclic structure in modular arithmetic. It is the special case of Euler's Theorem when the modulus is prime. It is also the shadow, at the level of exponents, of a deeper fact. That deeper fact is that the nonzero residues mod form a cyclic group under multiplication. This is the subject of The Order of an Element. It is also the engine behind fast primality tests, RSA-style cryptography, and countless olympiad divisibility tricks. This article states the theorem in both of its standard forms. It proves the theorem completely by two independent, elementary methods. It works through nontrivial numerical examples. It shows precisely which hypothesis, primality of the modulus, cannot be dropped.
- For prime and integer with : .
- Equivalent form valid for every integer (no coprimality needed): .
- Proved here two ways: via a permutation argument on residues, and via the Binomial Theorem and induction.
- The converse is false: numbers satisfying need not be prime (Fermat pseudoprimes, Carmichael numbers).
- Used to compute huge powers mod instantly, to build fast (probabilistic) primality tests, and in RSA cryptography.
- Key idea: multiplication by permutes the nonzero residues mod , so the product of all of them is unchanged up to the factor .
Where this lives today
Fermat's Little Theorem is a working tool wherever modular exponentiation appears. In cryptography, RSA encryption and decryption correctness rests on Euler's Theorem, whose proof and structure directly generalize the permutation argument given here; the special prime case is used explicitly when RSA primes are chosen and when computing modular inverses via . In algorithm design, it underlies probabilistic primality testing (the Fermat primality test, and its refinement the Miller–Rabin test), which checks for random bases to quickly rule out compositeness, accepting the theoretical caveat of pseudoprimes discussed above. In computer algebra and coding theory, fast modular exponentiation combined with Fermat's theorem gives efficient formulas for modular inverses and for evaluating polynomials over finite fields . In pure mathematics, it is the seed from which Euler's Theorem and the theory of the multiplicative order of an element — see The Order of an Element — grow, eventually leading to the structure theorem that the multiplicative group of nonzero residues mod a prime is cyclic.
Ask the tutor: How does the permutation argument used here generalize to prove Euler's Theorem for a composite modulus?
Let be a prime number and let .
- We say is coprime to , written , if does not divide (since is prime, this is the only way ).
- The congruence means .
- A complete residue system mod is any set of integers, one from each congruence class mod ; a reduced residue system mod (when is prime) is , i.e. representatives of the nonzero classes.
Fermat's Little Theorem is the pair of statements:
Intuition — prose building the mental picture
Fix a prime and think of multiplication by (with ) as a function acting on the set of nonzero residues modulo . The claim is that this function does nothing more than shuffle the set: every nonzero residue is hit exactly once. This is because if then, since has a modular inverse mod (as ), we can cancel to get . So the map is injective on a finite set, hence a bijection — a permutation of . Now multiply together all the outputs of this permutation. Since it is just a reshuffling, the product of the outputs equals the product of the inputs:
The left side is and the right side is . Since is prime and is a product of numbers each less than , none of its factors is divisible by , so is invertible mod and can be cancelled — leaving exactly . The picture below shows this shuffling concretely for , .
The second form, , is really the same statement dressed for all integers at once: if then both sides are trivially, and if we may multiply the first form by . Intuitively, the two statements say the same thing about a prime modulus; the difference is only whether you insist on cancelling the common factor first.
Let be a prime number.
(A) If and , then
(B) For every (with no coprimality assumption),
Hypotheses used essentially: is prime; in part (A), is coprime to (i.e. ).
We give two independent proofs of part (A), then deduce (B).
Proof 1 (permutation / product argument). Let be prime and let with . Step 1: the map is injective on . Suppose satisfy , i.e. . Since is prime and , Euclid's Lemma (if a prime divides a product it divides one of the factors) forces . As , we have , so forces . Thus the map is injective. Step 2: the map sends into itself, hence bijectively onto itself. For each , since and (as ), Euclid's Lemma again gives , so (it cannot be ). So the map sends the finite set into itself; being injective (Step 1) and finite, it is a bijection . Step 3: multiply everything together. Because the map is a bijection of , the multiset of values equals the multiset itself, just possibly reordered. Hence the products of the two multisets are congruent mod :
The left side factors as . So
i.e.
Step 4: cancel . Since is prime and each factor of is strictly between and , none of them is divisible by ; by repeated application of Euclid's Lemma, does not divide the product . Therefore , so has a modular inverse mod . Multiplying the congruence through by that inverse (equivalently: since is prime and with , Euclid's Lemma forces ) gives
This proves part (A). (Proof 1)
Proof 2 (binomial theorem + induction on ), proving (B) directly for . Base case. : (interpreting since ). True. Inductive step. Suppose for some integer ; we show . By the Binomial Theorem,
Claim: for , . Indeed,
and is divisible by . Since , both and are products of integers strictly less than , hence (as in Step 4 above) not divisible by the prime . So does not divide . Because and is an integer with , and , Euclid's Lemma applied to the equation shows must divide (the prime divides the right side exactly once as a factor times coprime to , and cannot be absorbed into the coprime factor on the left, so it must divide ). This proves the claim. Therefore every term in is divisible by , so
By the inductive hypothesis , hence
completing the induction. By induction, for all integers . Extending to negative . Let and write with . If : is a product of two consecutive integers, hence even, so directly. If is odd, since is odd, and by the case just proved , so
This covers all integers , proving (B) in full. (Proof 2)
Deducing (A) from (B), and (B) from (A), to record their equivalence. If , then from (B), . Since is prime and , Euclid's Lemma gives , i.e. , which is (A). Conversely if then trivially , and if then multiplying (A) by gives ; together these give (B) from (A). So (A) and (B) are logically equivalent statements, and we have now proved both, by two independent methods.
∎Let be prime, , and a positive integer. Write with (division with remainder). Then
This follows because by the theorem.
Worked examples
Example 1 — computing a genuinely large power. Compute . Here is prime and , so Fermat's Little Theorem gives . Divide the exponent: , so
Now compute directly by repeated squaring:
So . As a sanity check note so is correct, and so it is reduced. Without Fermat's theorem this would require handling a -fold multiplication; with it, the problem collapses to a small computation.
Example 2 — using Fermat's theorem to prove a divisibility statement. Show that for every integer . First factor , a product of distinct primes. By the Chinese Remainder Theorem, it suffices to show modulo each of separately, since if is divisible by each of five pairwise coprime numbers it is divisible by their product.
- Mod : by Fermat's theorem (form B) with , , so $n^{13} =
(n^2)^6\cdot n \equiv n^6\cdot n=n^7\pmod2n^7 \equiv n \pmod 2n\equiv n^2n\equiv n2n^2\equiv nn^k \equiv nk\ge1n^{k+1}=n\cdot n^k \equiv n\cdot n = n^2 \equiv nn^{13}\equiv n\pmod 2$.
- Mod : form (B) with gives . Then .
Since , induction as above gives for all (, and one checks separately fails in general — instead argue directly: , and since , we get ; more simply, ). So .
- Mod : form (B) with gives , and if form (A) gives
, so when ; if both sides are . Either way .
- Mod : if , both sides . If , form (A) with gives
, so .
- Mod : if , both sides are . If , form (A) with gives
, and , so . All five congruences hold for every integer , so by the Chinese Remainder Theorem, for every integer . This is a genuine, checkable identity: e.g. gives , confirming .
Drop the hypothesis that is prime and try (composite), (). Fermat's theorem would (falsely, if it held for composite moduli) predict . Compute directly:
so $2^{14} = 2^{12}\cdot2^2 = (2^4)^3\cdot 2^2 \equiv 1^3\cdot 4 = 4 \pmod{15}.
Scope, edge cases & common traps
| Situation | What happens |
|---|---|
| (form A) applies directly. | |
| Form (A) is not claimed (and in general); use form (B), , which still holds. | |
| composite | Form (A)/(B) need not hold; see the counterexample with . |
| Converse of (A) | False in general: there exist composite and coprime to with (Fermat pseudoprimes; Carmichael numbers such as fail it for every coprime ). |
| Exponent reduction | Only valid mod in the exponent when the base is coprime to ; e.g. , but requires . |
The mathematician behind it
Origins · 1 minPierre de Fermat and how this was discovered
Pierre de Fermat was a lawyer and magistrate at the Parlement of Toulouse who pursued mathematics as an intensely serious avocation, corresponding with contemporaries such as Mersenne, Pascal, and Frenicle de Bessy. Working essentially alone on number theory at a time when the subject had little institutional standing, he stated an extraordinary sequence of results — many without proof, communicated only in letters — that would occupy mathematicians for centuries, most famously his "Last Theorem." His "little theorem," communicated in a 1640 letter to Frenicle de Bessy, was stated without the general proof we now use; the first published proofs came later, notably from Leibniz (unpublished, circa 1683) and Euler (1736), who also generalized it to composite moduli.
Does Fermat's Little Theorem work for ?
Yes, but only in form (B). Since for any prime , we trivially get . Form (A), , explicitly requires ; since always, form (A) simply does not apply to , and indeed . This is exactly why the theorem is usually stated with the coprimality hypothesis attached to form (A), while form (B) is phrased to hold unconditionally for every integer, including and negative integers.
Is the converse of Fermat's Little Theorem true — does imply is prime?
No. Composite numbers can satisfy for some, or even for every, coprime to ; these are called Fermat pseudoprimes to base , and numbers that pass the test for every coprime base (like ) are Carmichael numbers. This is precisely why primality testing needs sharper tools, such as testing multiple bases, the Miller–Rabin test, or provable-primality certificates, rather than relying on a single Fermat check. The false converse is one of the most instructive cautionary examples in elementary number theory about not confusing a theorem with its converse.
How does Fermat's Little Theorem relate to Euler's Theorem?
Euler's Theorem generalizes form (A) to an arbitrary modulus (not necessarily prime): if then , where is Euler's totient function counting integers in coprime to . When is prime, , so Euler's Theorem specializes exactly to Fermat's Little Theorem. The proof technique is identical in spirit: multiplication by permutes the reduced residue system mod (the units), rather than all of , and the same cancellation argument applies. See Euler's Theorem for the full statement and proof.
Can Fermat's Little Theorem be used to find modular inverses?
Yes, when the modulus is prime. If , form (A) gives , so is a modular inverse of mod . This gives an alternative to the Extended Euclidean Algorithm for computing inverses, useful in settings (like modular exponentiation-heavy cryptographic code) where fast exponentiation by repeated squaring is already implemented; it is, however, generally slower than the Euclidean algorithm for a single inverse, since it requires multiplications versus the Euclidean algorithm's comparable cost without needing prime.
Does the theorem tell us anything about the order of modulo ?
Indirectly, yes, and this is the seed of the theory of The Order of an Element. The theorem shows the multiplicative order of mod (the smallest positive with ) must divide , because and a standard lemma shows any exponent making congruent to must be a multiple of the order. This one fact — order divides — is the starting point for showing the nonzero residues mod form a cyclic group, for constructing primitive roots, and for algorithms like the discrete logarithm and Pohlig–Hellman.
What is a quick way to remember which form needs coprimality?
Remember "the theorem with the minus one needs the extra hypothesis": requires , because otherwise the right side would falsely claim . The form without a minus one on the exponent, , needs no extra hypothesis because it degrades gracefully to when . If in doubt in an exam, default to using form (B) and only invoke form (A) after explicitly checking .