Fermat's Little Theorem

Take any prime pp and any integer aa not divisible by pp. Raise aa to the power p−1p-1 and reduce modulo pp. Astonishingly, the answer is always 11. Try p=7p=7, a=3a=3: the powers 3,9,27,81,243,7293,9,27,81,243,729 reduce mod 77 to 3,2,6,4,5,13,2,6,4,5,1. Indeed 36≡1(mod7)3^6 \equiv 1 \pmod 7. 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 pp form a finite algebraic system. In this system, every element has finite multiplicative order dividing p−1p-1. 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 pp 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.

Key takeaways
  • For prime pp and integer aa with p∤ap \nmid a: ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p.
  • Equivalent form valid for every integer aa (no coprimality needed): ap≡a(modp)a^p \equiv a \pmod p.
  • Proved here two ways: via a permutation argument on residues, and via the Binomial Theorem and induction.
  • The converse is false: numbers satisfying an−1≡1(modn)a^{n-1}\equiv 1\pmod n need not be prime (Fermat pseudoprimes, Carmichael numbers).
  • Used to compute huge powers mod pp instantly, to build fast (probabilistic) primality tests, and in RSA cryptography.
  • Key idea: multiplication by aa permutes the nonzero residues mod pp, so the product of all of them is unchanged up to the factor ap−1a^{p-1}.

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 a−1≡ap−2(modp)a^{-1}\equiv a^{p-2}\pmod p. In algorithm design, it underlies probabilistic primality testing (the Fermat primality test, and its refinement the Miller–Rabin test), which checks an−1  na^{n-1}\bmod n for random bases aa 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 Fp\mathbb{F}_p. 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?

DefinitionFermat's Little Theorem — statement of the objects involved

Let pp be a prime number and let a∈Za\in\mathbb{Z}.

  • We say aa is coprime to pp, written gcd⁡(a,p)=1\gcd(a,p)=1, if pp does not divide aa (since pp is prime, this is the only way gcd⁡(a,p)≠p\gcd(a,p)\neq p).
  • The congruence x≡y(modp)x\equiv y\pmod p means p∣(x−y)p\mid (x-y).
  • A complete residue system mod pp is any set of pp integers, one from each congruence class mod pp; a reduced residue system mod pp (when pp is prime) is {1,2,…,p−1}\{1,2,\dots,p-1\}, i.e. representatives of the p−1p-1 nonzero classes.

Fermat's Little Theorem is the pair of statements:

ap−1≡1(modp)whenever p∤a,a^{p-1}\equiv 1 \pmod p \quad \text{whenever } p\nmid a,
ap≡a(modp)for every integer a.a^{p}\equiv a \pmod p \quad \text{for every integer } a.

Intuition — prose building the mental picture

Fix a prime pp and think of multiplication by aa (with p∤ap\nmid a) as a function acting on the set of nonzero residues {1,2,…,p−1}\{1,2,\dots,p-1\} modulo pp. The claim is that this function does nothing more than shuffle the set: every nonzero residue is hit exactly once. This is because if a⋅x≡a⋅y(modp)a\cdot x \equiv a\cdot y \pmod p then, since aa has a modular inverse mod pp (as gcd⁡(a,p)=1\gcd(a,p)=1), we can cancel aa to get x≡yx\equiv y. So the map x↦ax  px \mapsto ax \bmod p is injective on a finite set, hence a bijection — a permutation of {1,…,p−1}\{1,\dots,p-1\}. 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:

(a⋅1)(a⋅2)⋯(a⋅(p−1))≡1⋅2⋯(p−1)(modp).(a\cdot 1)(a\cdot 2)\cdots(a\cdot(p-1)) \equiv 1\cdot 2\cdots (p-1) \pmod p.

The left side is ap−1 (p−1)!a^{p-1}\,(p-1)! and the right side is (p−1)!(p-1)!. Since pp is prime and (p−1)!(p-1)! is a product of numbers each less than pp, none of its factors is divisible by pp, so (p−1)!(p-1)! is invertible mod pp and can be cancelled — leaving exactly ap−1≡1(modp)a^{p-1}\equiv 1\pmod p. The picture below shows this shuffling concretely for p=7p=7, a=3a=3.

Figure
Multiplication by a=3a=3 permutes the nonzero residues mod 77
Residues mod 7, top row = input x, bottom row = 3x mod 7 1 2 3 4 5 6 3 6 2 5 1 4 Same six numbers appear in both rows — just reordered, so the products agree.

The second form, ap≡a(modp)a^p\equiv a\pmod p, is really the same statement dressed for all integers at once: if p∣ap\mid a then both sides are 0(modp)0\pmod p trivially, and if p∤ap\nmid a we may multiply the first form by aa. Intuitively, the two statements say the same thing about a prime modulus; the difference is only whether you insist on cancelling the common factor aa first.

TheoremFermat's Little Theorem

Let pp be a prime number.

(A) If a∈Za\in\mathbb{Z} and p∤ap\nmid a, then

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

(B) For every a∈Za\in\mathbb{Z} (with no coprimality assumption),

ap≡a(modp).a^{p}\equiv a \pmod p.

Hypotheses used essentially: pp is prime; in part (A), aa is coprime to pp (i.e. p∤ap\nmid a).

Proof

We give two independent proofs of part (A), then deduce (B).

Proof 1 (permutation / product argument). Let pp be prime and let a∈Za\in\mathbb{Z} with p∤ap \nmid a. Step 1: the map x↦ax  px\mapsto ax \bmod p is injective on {1,2,…,p−1}\{1,2,\dots,p-1\}. Suppose x,y∈{1,…,p−1}x,y\in\{1,\dots,p-1\} satisfy ax≡ay(modp)ax\equiv ay\pmod p, i.e. p∣a(x−y)p\mid a(x-y). Since pp is prime and p∤ap\nmid a, Euclid's Lemma (if a prime divides a product it divides one of the factors) forces p∣(x−y)p\mid (x-y). As x,y∈{1,…,p−1}x,y\in\{1,\dots,p-1\}, we have ∣x−y∣<p|x-y|<p, so p∣(x−y)p\mid(x-y) forces x=yx=y. Thus the map is injective. Step 2: the map sends {1,…,p−1}\{1,\dots,p-1\} into itself, hence bijectively onto itself. For each x∈{1,…,p−1}x\in\{1,\dots,p-1\}, since p∤ap\nmid a and p∤xp\nmid x (as 1≤x≤p−11\le x\le p-1), Euclid's Lemma again gives p∤axp \nmid ax, so ax  p∈{1,…,p−1}ax \bmod p \in\{1,\dots,p-1\} (it cannot be 00). So the map x↦ax  px\mapsto ax\bmod p sends the finite set S={1,…,p−1}S=\{1,\dots,p-1\} into itself; being injective (Step 1) and SS finite, it is a bijection S→SS\to S. Step 3: multiply everything together. Because the map is a bijection of SS, the multiset of values {ax  p:x∈S}\{ax \bmod p : x\in S\} equals the multiset SS itself, just possibly reordered. Hence the products of the two multisets are congruent mod pp:

∏x=1p−1(ax)  ≡  ∏x=1p−1x(modp).\prod_{x=1}^{p-1} (ax) \;\equiv\; \prod_{x=1}^{p-1} x \pmod p.

The left side factors as ap−1∏x=1p−1x=ap−1(p−1)!a^{p-1}\displaystyle\prod_{x=1}^{p-1}x = a^{p-1}(p-1)!. So

ap−1 (p−1)!≡(p−1)!(modp),a^{p-1}\,(p-1)! \equiv (p-1)! \pmod p,

i.e.

p∣(p−1)! (ap−1−1).p \mid (p-1)!\,\bigl(a^{p-1}-1\bigr).

Step 4: cancel (p−1)!(p-1)!. Since pp is prime and each factor 1,2,…,p−11,2,\dots,p-1 of (p−1)!(p-1)! is strictly between 00 and pp, none of them is divisible by pp; by repeated application of Euclid's Lemma, pp does not divide the product (p−1)!(p-1)!. Therefore gcd⁡(p,(p−1)!)=1\gcd\big(p,(p-1)!\big)=1, so (p−1)!(p-1)! has a modular inverse mod pp. Multiplying the congruence p∣(p−1)! (ap−1−1)p \mid (p-1)!\,(a^{p-1}-1) through by that inverse (equivalently: since pp is prime and p∣(p−1)!(ap−1−1)p\mid (p-1)!(a^{p-1}-1) with p∤(p−1)!p\nmid (p-1)!, Euclid's Lemma forces p∣ap−1−1p \mid a^{p-1}-1) gives

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

This proves part (A). ■\blacksquare (Proof 1)

Proof 2 (binomial theorem + induction on aa), proving (B) directly for a≥0a\ge 0. Base case. a=0a=0: 0p=0≡0(modp)0^p = 0 \equiv 0 \pmod p (interpreting 0p=00^p=0 since p≥2>0p\ge 2>0). True. Inductive step. Suppose ap≡a(modp)a^p \equiv a \pmod p for some integer a≥0a\ge 0; we show (a+1)p≡a+1(modp)(a+1)^p \equiv a+1 \pmod p. By the Binomial Theorem,

(a+1)p=∑k=0p(pk)ak=ap+1+∑k=1p−1(pk)ak.(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.

Claim: for 1≤k≤p−11\le k\le p-1, p∣(pk)p \mid \binom{p}{k}. Indeed,

(pk)=p!k!(p−k)!,\binom{p}{k} = \frac{p!}{k!(p-k)!},

and k!(p−k)!(pk)=p!k!(p-k)!\binom{p}{k}=p! is divisible by pp. Since 1≤k≤p−11\le k\le p-1, both k!k! and (p−k)!(p-k)! are products of integers strictly less than pp, hence (as in Step 4 above) not divisible by the prime pp. So pp does not divide k!(p−k)!k!(p-k)!. Because k!(p−k)!∣p⋅(p−1)!k!(p-k)!\mid p\cdot(p-1)! and (pk)\binom{p}{k} is an integer with k!(p−k)!(pk)=p⋅(p−1)!k!(p-k)!\binom{p}{k} = p\cdot (p-1)!, and p∤k!(p−k)!p \nmid k!(p-k)!, Euclid's Lemma applied to the equation (pk)⋅(k!(p−k)!)=p⋅(p−1)!\binom{p}{k}\cdot\bigl(k!(p-k)!\bigr) = p\cdot(p-1)! shows pp must divide (pk)\binom{p}{k} (the prime pp divides the right side exactly once as a factor times (p−1)!(p-1)! coprime to pp, and cannot be absorbed into the coprime factor k!(p−k)!k!(p-k)! on the left, so it must divide (pk)\binom{p}{k}). This proves the claim. Therefore every term in ∑k=1p−1(pk)ak\sum_{k=1}^{p-1}\binom{p}{k}a^k is divisible by pp, so

(a+1)p≡ap+1(modp).(a+1)^p \equiv a^p + 1 \pmod p.

By the inductive hypothesis ap≡a(modp)a^p\equiv a\pmod p, hence

(a+1)p≡a+1(modp),(a+1)^p \equiv a+1 \pmod p,

completing the induction. By induction, ap≡a(modp)a^p\equiv a\pmod p for all integers a≥0a\ge 0. Extending to negative aa. Let a<0a<0 and write a=−ba=-b with b>0b>0. If p=2p=2: a2−a=a(a−1)a^2 - a = a(a-1) is a product of two consecutive integers, hence even, so a2≡a(mod2)a^2\equiv a\pmod 2 directly. If pp is odd, (−b)p=−bp(-b)^p = -b^p since pp is odd, and by the case just proved bp≡b(modp)b^p\equiv b\pmod p, so

ap=(−b)p=−bp≡−b=a(modp).a^p = (-b)^p = -b^p \equiv -b = a \pmod p.

This covers all integers aa, proving (B) in full. ■\blacksquare (Proof 2)

Deducing (A) from (B), and (B) from (A), to record their equivalence. If p∤ap\nmid a, then from (B), p∣ap−a=a(ap−1−1)p \mid a^p - a = a(a^{p-1}-1). Since pp is prime and p∤ap\nmid a, Euclid's Lemma gives p∣ap−1−1p\mid a^{p-1}-1, i.e. ap−1≡1(modp)a^{p-1}\equiv 1\pmod p, which is (A). Conversely if p∣ap\mid a then trivially ap≡0≡a(modp)a^p\equiv 0\equiv a\pmod p, and if p∤ap\nmid a then multiplying (A) by aa gives ap≡a(modp)a^{p}\equiv a\pmod p; together these give (B) from (A). So (A) and (B) are logically equivalent statements, and we have now proved both, by two independent methods. ■\blacksquare

∎
CorollaryComputing large powers mod pp

Let pp be prime, p∤ap\nmid a, and nn a positive integer. Write n=q(p−1)+rn = q(p-1)+r with 0≤r<p−10\le r < p-1 (division with remainder). Then

an≡ar(modp).a^{n} \equiv a^{r} \pmod p.

This follows because an=(ap−1)q⋅ar≡1q⋅ar=ar(modp)a^n = (a^{p-1})^q\cdot a^r \equiv 1^q \cdot a^r = a^r \pmod p by the theorem.

Worked examples

Example 1 — computing a genuinely large power. Compute 7222  137^{222} \bmod 13. Here p=13p=13 is prime and gcd⁡(7,13)=1\gcd(7,13)=1, so Fermat's Little Theorem gives 712≡1(mod13)7^{12}\equiv 1 \pmod{13}. Divide the exponent: 222=18⋅12+6222 = 18\cdot 12 + 6, so

7222=(712)18⋅76≡118⋅76=76(mod13).7^{222} = (7^{12})^{18}\cdot 7^{6} \equiv 1^{18}\cdot 7^{6} = 7^{6}\pmod{13}.

Now compute 76  137^6 \bmod 13 directly by repeated squaring:

72=49≡49−39=10(mod13),7^2 = 49 \equiv 49 - 39 = 10 \pmod{13},
74≡102=100≡100−91=9(mod13),7^4 \equiv 10^2 = 100 \equiv 100 - 91 = 9 \pmod{13},
76=74⋅72≡9⋅10=90≡90−78=12(mod13).7^6 = 7^4\cdot 7^2 \equiv 9\cdot 10 = 90 \equiv 90 - 78 = 12 \pmod{13}.

So 7222≡12≡−1(mod13)7^{222}\equiv 12 \equiv -1 \pmod{13}. As a sanity check note 13∣7813\mid 78 so 90−78=1290-78=12 is correct, and 12<1312<13 so it is reduced. Without Fermat's theorem this would require handling a 222222-fold multiplication; with it, the problem collapses to a small computation.

Example 2 — using Fermat's theorem to prove a divisibility statement. Show that n13≡n(mod2730)n^{13} \equiv n \pmod{2730} for every integer nn. First factor 2730=2⋅3⋅5⋅7⋅132730 = 2\cdot 3\cdot 5\cdot 7\cdot 13, a product of distinct primes. By the Chinese Remainder Theorem, it suffices to show n13≡nn^{13}\equiv n modulo each of 2,3,5,7,132,3,5,7,13 separately, since if n13−nn^{13}-n is divisible by each of five pairwise coprime numbers it is divisible by their product.

  • Mod 22: by Fermat's theorem (form B) with p=2p=2, n2≡n(mod2)n^2\equiv n\pmod 2, so $n^{13} =

(n^2)^6\cdot n \equiv n^6\cdot n=n^7\pmod2;repeating,; repeating, n^7 \equiv n \pmod 2(sincesquaringpreserves (since squaring preserves n\equiv n^2,anypowerof, any power of nis is \equiv nmod mod 2once once n^2\equiv n:indeed: indeed n^k \equiv nforall for all k\ge1byinduction,since by induction, since n^{k+1}=n\cdot n^k \equiv n\cdot n = n^2 \equiv n).So). So n^{13}\equiv n\pmod 2$.

  • Mod 33: form (B) with p=3p=3 gives n3≡n(mod3)n^3\equiv n\pmod 3. Then n13=(n3)4⋅nn^{13} = (n^3)^4\cdot n.

Since n3≡nn^3\equiv n, induction as above gives nk≡n(mod3)n^k\equiv n\pmod3 for all k≥1k\ge 1 (nk+1=n⋅nk≡n⋅n=n2n^{k+1}=n\cdot n^k\equiv n\cdot n=n^2, and one checks separately n2≡n(mod3)n^2\equiv n\pmod3 fails in general — instead argue directly: 13=3⋅4+113 = 3\cdot 4+1, and since n3≡nn^{3}\equiv n, we get n2⋅3+1=n3⋅n3⋅n≡n⋅n⋅n=n3≡nn^{2\cdot3+1}=n^{3}\cdot n^3\cdot n \equiv n\cdot n\cdot n = n^3\equiv n; more simply, n13=(n3)4⋅n≡n4⋅n=n5=(n3)⋅n2≡n⋅n2=n3≡n(mod3)n^{13}=(n^3)^4\cdot n\equiv n^4\cdot n=n^5=(n^3)\cdot n^2\equiv n\cdot n^2=n^3\equiv n\pmod3). So n13≡n(mod3)n^{13}\equiv n\pmod 3.

  • Mod 55: form (B) with p=5p=5 gives n5≡n(mod5)n^5\equiv n\pmod5, and if 5∤n5\nmid n form (A) gives

n4≡1n^4\equiv1, so n13=n4⋅3⋅n≡n(mod5)n^{13}=n^{4\cdot3}\cdot n \equiv n \pmod5 when 5∤n5\nmid n; if 5∣n5\mid n both sides are 0  50\bmod5. Either way n13≡n(mod5)n^{13}\equiv n\pmod5.

  • Mod 77: if 7∣n7\mid n, both sides ≡0\equiv 0. If 7∤n7\nmid n, form (A) with p=7p=7 gives

n6≡1(mod7)n^6\equiv1\pmod7, so n13=n6⋅2⋅n≡n(mod7)n^{13} = n^{6\cdot2}\cdot n \equiv n\pmod 7.

  • Mod 1313: if 13∣n13\mid n, both sides are 00. If 13∤n13\nmid n, form (A) with p=13p=13 gives

n12≡1(mod13)n^{12}\equiv 1\pmod{13}, and 13=12⋅1+113 = 12\cdot1+1, so n13=n12⋅n≡n(mod13)n^{13} = n^{12}\cdot n \equiv n \pmod{13}. All five congruences hold for every integer nn, so by the Chinese Remainder Theorem, n13≡n(mod2730)n^{13}\equiv n \pmod{2730} for every integer nn. This is a genuine, checkable identity: e.g. n=2n=2 gives 213=8192=3⋅2730+22^{13}=8192 = 3\cdot2730+2, confirming 8192≡2(mod2730)8192\equiv 2\pmod{2730}.

Counterexamplewhy primality of the modulus is essential

Drop the hypothesis that pp is prime and try n=15n=15 (composite), a=2a=2 (gcd⁡(2,15)=1\gcd(2,15)=1). Fermat's theorem would (falsely, if it held for composite moduli) predict 214≡1(mod15)2^{14}\equiv 1\pmod{15}. Compute directly:

24=16≡1(mod15),2^4 = 16 \equiv 1 \pmod{15},

so $2^{14} = 2^{12}\cdot2^2 = (2^4)^3\cdot 2^2 \equiv 1^3\cdot 4 = 4 \pmod{15}.

Indeed $2^{14}=16384 = 1092\cdot15 + 4$, confirming $2^{14}\equiv 4\pmod{15}$, **not** $1$. The statement "$a^{n-1}\equiv1\pmod n$ whenever $\gcd(a,n)=1$" simply fails for the composite modulus $n=15$ and base $a=2$. The reason the proof breaks: in Step 4 of Proof 1, we needed $p$ prime to guarantee $(p-1)!$ (or the analogous product of units) is coprime to the modulus, and in Proof 2 we needed $p$ prime to guarantee every middle binomial coefficient $\binom{p}{k}$, $1\le k\le p-1$, is divisible by $p$ — both facts are false for composite moduli. (For example $\binom{4}{2}=6$ is not divisible by $4$.) This is exactly why the correct generalization to arbitrary moduli, replacing $p-1$ by the number of units mod $n$, is [[Euler's Theorem]] rather than a literal restatement of Fermat's.

Scope, edge cases & common traps

SituationWhat happens
p∤ap\nmid aap−1≡1(modp)a^{p-1}\equiv 1\pmod p (form A) applies directly.
p∣ap\mid aForm (A) is not claimed (and ap−1≡0≢1a^{p-1}\equiv 0\not\equiv1 in general); use form (B), ap≡a≡0(modp)a^p\equiv a\equiv 0\pmod p, which still holds.
nn compositeForm (A)/(B) need not hold; see the counterexample with n=15,a=2n=15,a=2.
Converse of (A)False in general: there exist composite nn and aa coprime to nn with an−1≡1(modn)a^{n-1}\equiv1\pmod n (Fermat pseudoprimes; Carmichael numbers such as 561561 fail it for every coprime aa).
Exponent reductionOnly valid mod p−1p-1 in the exponent when the base is coprime to pp; e.g. a2p−2≡1a^{2p-2}\equiv1, but ap−1+r≡ara^{p-1+r}\equiv a^r requires p∤ap\nmid a.

The mathematician behind it

Origins · 1 minPierre de Fermat and how this was discovered
PD
Pierre de Fermat
1607–1665 · Number theory in early modern France

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.

History
- 1640 — Fermat states the theorem (without a general proof) in a letter to Frenicle de Bessy.
- 1683 (circa) — Leibniz gives an essentially complete proof in unpublished manuscripts.
- 1736 — Euler publishes the first proof to appear in print.
- 1760 — Euler publishes the generalization to composite moduli, now called Euler's Theorem.
- 1801 — Gauss reformulates and systematizes the result in Disquisitiones Arithmeticae using the language of congruences.
Does Fermat's Little Theorem work for a=0a=0?

Yes, but only in form (B). Since 0p=00^p = 0 for any prime p≥2p\ge2, we trivially get 0p≡0(modp)0^p\equiv0\pmod p. Form (A), ap−1≡1(modp)a^{p-1}\equiv1\pmod p, explicitly requires p∤ap\nmid a; since p∣0p\mid 0 always, form (A) simply does not apply to a=0a=0, and indeed 0p−1=0≢1(modp)0^{p-1}=0\not\equiv1\pmod p. 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 00 and negative integers.

Is the converse of Fermat's Little Theorem true — does an−1≡1(modn)a^{n-1}\equiv1\pmod n imply nn is prime?

No. Composite numbers nn can satisfy an−1≡1(modn)a^{n-1}\equiv1\pmod n for some, or even for every, aa coprime to nn; these are called Fermat pseudoprimes to base aa, and numbers that pass the test for every coprime base (like 561=3⋅11⋅17561 = 3\cdot11\cdot17) 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 nn (not necessarily prime): if gcd⁡(a,n)=1\gcd(a,n)=1 then aφ(n)≡1(modn)a^{\varphi(n)}\equiv1\pmod n, where φ(n)\varphi(n) is Euler's totient function counting integers in {1,…,n}\{1,\dots,n\} coprime to nn. When n=pn=p is prime, φ(p)=p−1\varphi(p)=p-1, so Euler's Theorem specializes exactly to Fermat's Little Theorem. The proof technique is identical in spirit: multiplication by aa permutes the reduced residue system mod nn (the units), rather than all of {1,…,n−1}\{1,\dots,n-1\}, 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 pp is prime. If p∤ap\nmid a, form (A) gives a⋅ap−2≡ap−1≡1(modp)a\cdot a^{p-2}\equiv a^{p-1}\equiv1\pmod p, so ap−2  pa^{p-2}\bmod p is a modular inverse of aa mod pp. 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 O(log⁡p)O(\log p) multiplications versus the Euclidean algorithm's comparable cost without needing pp prime.

Does the theorem tell us anything about the order of aa modulo pp?

Indirectly, yes, and this is the seed of the theory of The Order of an Element. The theorem shows the multiplicative order of aa mod pp (the smallest positive kk with ak≡1(modp)a^k\equiv1\pmod p) must divide p−1p-1, because ap−1≡1a^{p-1}\equiv1 and a standard lemma shows any exponent making aa congruent to 11 must be a multiple of the order. This one fact — order divides p−1p-1 — is the starting point for showing the nonzero residues mod pp 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": ap−1≡1(modp)a^{p-1}\equiv 1\pmod p requires p∤ap\nmid a, because otherwise the right side would falsely claim 0≡10\equiv1. The form without a minus one on the exponent, ap≡a(modp)a^p\equiv a\pmod p, needs no extra hypothesis because it degrades gracefully to 0≡0(modp)0\equiv0\pmod p when p∣ap\mid a. If in doubt in an exam, default to using form (B) and only invoke form (A) after explicitly checking p∤ap\nmid a.

Before this

Where this leads