Posted in

Fermat’s Little Theorem Explained: Definition, Proof and Applications

Fermat’s Little Theorem is one of the most elegant and useful results in number theory. It gives a clean, powerful way to compute large powers modulo a prime — turning what might seem like an impossibly large calculation into a simple remainder problem. It appears in the Euclid Contest, the COMC, and the Canadian Mathematical Olympiad, and it is the foundation for several important results in modern cryptography, including RSA encryption.

This guide explains Fermat’s Little Theorem clearly, proves it in two ways, works through examples at increasing difficulty, and shows how to apply it strategically in competition problems.


What Is Fermat’s Little Theorem?

Fermat’s Little Theorem states:

If ppp is a prime number and aaa is an integer not divisible by ppp, then:ap11(modp)a^{p-1} \equiv 1 \pmod{p}ap−1≡1(modp)

Equivalently (and more generally, without the divisibility condition):apa(modp)a^p \equiv a \pmod{p}ap≡a(modp)

In plain English: Raise any integer aaa to the power ppp (where ppp is prime) and divide by ppp — the remainder is aaa itself. Or: raise aaa to the power p1p – 1p−1 (when ppp does not divide aaa) — the remainder when divided by ppp is always 1.

A first example:

Let p=7p = 7p=7 and a=2a = 2a=2.

271=26=64=9×7+12^{7-1} = 2^6 = 64 = 9 \times 7 + 127−1=26=64=9×7+1, so 261(mod7)2^6 \equiv 1 \pmod{7}26≡1(mod7). ✓

27=128=18×7+22^7 = 128 = 18 \times 7 + 227=128=18×7+2, so 272(mod7)2^7 \equiv 2 \pmod{7}27≡2(mod7). ✓

Another example:

Let p=5p = 5p=5 and a=3a = 3a=3.

34=81=16×5+13^4 = 81 = 16 \times 5 + 134=81=16×5+1, so 341(mod5)3^4 \equiv 1 \pmod{5}34≡1(mod5). ✓

The theorem works for every prime ppp and every integer aaa not divisible by ppp — always.


A Brief History

Pierre de Fermat stated this theorem in a letter in 1640 — without proof. (Fermat was notorious for claiming results without publishing proofs; his marginal note about Fermat’s Last Theorem is the most famous example.) The first published proof was given by Leonhard Euler in 1736, nearly a century later.

The name “Little” Theorem distinguishes it from Fermat’s Last Theorem (an+bn=cna^n + b^n = c^nan+bn=cn has no positive integer solutions for n3n \geq 3n≥3) — which is little in the sense of being easier to state, not in the sense of being less important. In practice, Fermat’s Little Theorem is far more useful in everyday mathematics than the Last Theorem.


Two Proofs of Fermat’s Little Theorem

Proof 1: The Necklace Argument (Most Elegant)

Consider sequences of length ppp using aaa colours (where ppp is prime and a>0a > 0a>0). The number of such sequences is apa^pap.

Of these, exactly aaa are constant sequences (all the same colour). The remaining apaa^p – aap−a sequences are non-constant.

Now arrange each non-constant sequence in a circle (a necklace). Each necklace corresponds to exactly ppp sequences (the ppp rotations of the circular arrangement) — and these are all distinct because ppp is prime and the sequence is non-constant.

Therefore apaa^p – aap−a is divisible by ppp:papa    apa(modp)p \mid a^p – a \implies a^p \equiv a \pmod{p}p∣ap−a⟹ap≡a(modp)

This proves the second (more general) form of Fermat’s Little Theorem. \square

Why primality matters: If ppp were not prime — say p=4p = 4p=4 — the rotations of a non-constant circular sequence might not all be distinct (e.g., the sequence ABAB has period 2, so its 4 rotations give only 2 distinct sequences). The argument breaks down. The theorem is specifically about primes.


Proof 2: The Cancellation Argument (Standard Algebraic Proof)

We prove ap11(modp)a^{p-1} \equiv 1 \pmod{p}ap−1≡1(modp) for gcd(a,p)=1\gcd(a, p) = 1gcd(a,p)=1.

Consider the p1p – 1p−1 numbers: a,2a,3a,,(p1)aa, 2a, 3a, \ldots, (p-1)aa,2a,3a,…,(p−1)a modulo ppp.

Claim: These are all distinct modulo ppp, and none is 0\equiv 0≡0.

Distinct: If iaja(modp)ia \equiv ja \pmod{p}ia≡ja(modp) for 1i<jp11 \leq i < j \leq p-11≤i<j≤p−1, then p(ji)ap \mid (j-i)ap∣(j−i)a. Since gcd(a,p)=1\gcd(a,p) = 1gcd(a,p)=1 and 0<ji<p0 < j-i < p0<j−i<p, this is impossible. So all values are distinct.

Non-zero: Since gcd(a,p)=1\gcd(a,p) = 1gcd(a,p)=1, none of a,2a,,(p1)aa, 2a, \ldots, (p-1)aa,2a,…,(p−1)a is divisible by ppp.

Therefore {a,2a,,(p1)a}{1,2,,p1}(modp)\{a, 2a, \ldots, (p-1)a\} \equiv \{1, 2, \ldots, p-1\} \pmod{p}{a,2a,…,(p−1)a}≡{1,2,…,p−1}(modp) (as sets).

Multiplying all elements of each set:a2a3a(p1)a123(p1)(modp)a \cdot 2a \cdot 3a \cdots (p-1)a \equiv 1 \cdot 2 \cdot 3 \cdots (p-1) \pmod{p}a⋅2a⋅3a⋯(p−1)a≡1⋅2⋅3⋯(p−1)(modp) ap1(p1)!(p1)!(modp)a^{p-1} \cdot (p-1)! \equiv (p-1)! \pmod{p}ap−1⋅(p−1)!≡(p−1)!(modp)

Since gcd((p1)!,p)=1\gcd((p-1)!, p) = 1gcd((p−1)!,p)=1, we can cancel (p1)!(p-1)!(p−1)!:ap11(modp)a^{p-1} \equiv 1 \pmod{p} \quad \squareap−1≡1(modp)□


How to Use Fermat’s Little Theorem: Computing Large Powers

The most immediate application of Fermat’s Little Theorem is computing an(modp)a^n \pmod{p}an(modp) for large nnn.

Method:

  1. Reduce the exponent nnn modulo p1p – 1p−1 (since ap11a^{p-1} \equiv 1ap−1≡1, the powers repeat with period p1p – 1p−1).
  2. If n=q(p1)+rn = q(p-1) + rn=q(p−1)+r with 0r<p10 \leq r < p-10≤r<p−1, then an=(ap1)qar1qarar(modp)a^n = (a^{p-1})^q \cdot a^r \equiv 1^q \cdot a^r \equiv a^r \pmod{p}an=(ap−1)q⋅ar≡1q⋅ar≡ar(modp).
  3. Compute ar(modp)a^r \pmod{p}ar(modp) directly (since r<p1r < p – 1r<p−1, this is manageable).

Example 1: Find 2100(mod7)2^{100} \pmod{7}2100(mod7).

p=7p = 7p=7, so ap1=261(mod7)a^{p-1} = 2^6 \equiv 1 \pmod{7}ap−1=26≡1(mod7).

100=16×6+4100 = 16 \times 6 + 4100=16×6+4, so 2100=(26)162411616162(mod7)2^{100} = (2^6)^{16} \cdot 2^4 \equiv 1^{16} \cdot 16 \equiv 16 \equiv 2 \pmod{7}2100=(26)16⋅24≡116⋅16≡16≡2(mod7).

Answer: 2100≡2(mod7)2^{100} \equiv 2 \pmod{7} 2100≡2(mod7).


Example 2: Find 3200(mod11)3^{200} \pmod{11}3200(mod11).

p=11p = 11p=11, so 3101(mod11)3^{10} \equiv 1 \pmod{11}310≡1(mod11).

200=20×10+0200 = 20 \times 10 + 0200=20×10+0, so 3200=(310)20120=1(mod11)3^{200} = (3^{10})^{20} \equiv 1^{20} = 1 \pmod{11}3200=(310)20≡120=1(mod11).

Answer: 3200≡1(mod11)3^{200} \equiv 1 \pmod{11} 3200≡1(mod11).


Example 3: Find 752(mod13)7^{52} \pmod{13}752(mod13).

p=13p = 13p=13, so 7121(mod13)7^{12} \equiv 1 \pmod{13}712≡1(mod13).

52=4×12+452 = 4 \times 12 + 452=4×12+4, so 75274(mod13)7^{52} \equiv 7^4 \pmod{13}752≡74(mod13).

72=49=3×13+107^2 = 49 = 3 \times 13 + 1072=49=3×13+10, so 72103(mod13)7^2 \equiv 10 \equiv -3 \pmod{13}72≡10≡−3(mod13).

74=(72)2(3)2=9(mod13)7^4 = (7^2)^2 \equiv (-3)^2 = 9 \pmod{13}74=(72)2≡(−3)2=9(mod13).

Answer: 752≡9(mod13)7^{52} \equiv 9 \pmod{13} 752≡9(mod13).

Competition math rewards students who recognise the right shortcut—not those who attempt the longest calculation. Our free assessment shows how confidently your child applies mathematical techniques to unfamiliar problems.

Get your child’s free feedback report →


Finding the Last Digits of Large Powers

Fermat’s Little Theorem handles modulo ppp for prime ppp. The most common “last digit” problems use modulo 10, but since 10 is not prime, Fermat’s Little Theorem does not apply directly. Instead, use modulo 2 and modulo 5 separately, then combine with the Chinese Remainder Theorem.

However, for problems specifically modulo a prime (modulo 7, 11, 13, 17, etc.), Fermat’s Little Theorem is the direct tool.

Example 4: What is the remainder when 6836^{83}683 is divided by 7?

61(mod7)6 \equiv -1 \pmod{7}6≡−1(mod7), so 683(1)83=16(mod7)6^{83} \equiv (-1)^{83} = -1 \equiv 6 \pmod{7}683≡(−1)83=−1≡6(mod7).

(Here we did not even need Fermat’s Little Theorem — the base 616 \equiv -16≡−1 mod 7 makes the power trivial. This illustrates that sometimes simpler observations suffice.)

Example 5: What is the remainder when 51005^{100}5100 is divided by 11?

p=11p = 11p=11, so 5101(mod11)5^{10} \equiv 1 \pmod{11}510≡1(mod11).

100=10×10100 = 10 \times 10100=10×10, so 5100=(510)10110=1(mod11)5^{100} = (5^{10})^{10} \equiv 1^{10} = 1 \pmod{11}5100=(510)10≡110=1(mod11).

Remainder = 1.


Extended Applications

Wilson’s Theorem (Companion Result)

Wilson’s Theorem states: ppp is prime if and only if (p1)!1(modp)(p-1)! \equiv -1 \pmod{p}(p−1)!≡−1(modp).

This is a companion to Fermat’s Little Theorem — both involve primes and modular arithmetic, and both appear in competition number theory. The proof of Wilson’s Theorem uses the cancellation argument in the Fermat proof as a model.

Euler’s Generalisation

Euler generalised Fermat’s Little Theorem to non-prime moduli using the Euler totient function ϕ(n)\phi(n)ϕ(n) — the count of integers from 1 to nnn coprime to nnn:aϕ(n)1(modn)when gcd(a,n)=1a^{\phi(n)} \equiv 1 \pmod{n} \quad \text{when } \gcd(a, n) = 1aϕ(n)≡1(modn)when gcd(a,n)=1

When n=pn = pn=p (prime), ϕ(p)=p1\phi(p) = p – 1ϕ(p)=p−1, recovering Fermat’s Little Theorem. Euler’s generalisation is the foundation of RSA cryptography.

Primality Testing

Fermat’s Little Theorem provides a quick (but imperfect) primality test: if an11(modn)a^{n-1} \not\equiv 1 \pmod{n}an−1≡1(modn) for some aaa with gcd(a,n)=1\gcd(a, n) = 1gcd(a,n)=1, then nnn is definitely not prime. This is the Fermat primality test.

The test is not foolproof — there exist composite numbers nnn (called Carmichael numbers) for which an11(modn)a^{n-1} \equiv 1 \pmod{n}an−1≡1(modn) for all gcd(a,n)=1\gcd(a,n) = 1gcd(a,n)=1. The smallest is 561 = 3 × 11 × 17. But as a quick filter for competition problems, the Fermat test is useful.


Where Fermat’s Little Theorem Appears in Contests

Fermat’s Little Theorem is standard toolkit at the senior competition level.

Euclid Contest (CEMC, Grade 12): Number theory problems appear in every Euclid, and computing powers modulo a prime is a recurring theme. A student who knows Fermat’s Little Theorem solves these in seconds; a student who does not may attempt direct calculation (impossible for large exponents) or pattern-finding (slower and less reliable). See our Euclid math contest guide.

COMC Part C: Divisibility proofs and modular arithmetic arguments appear at every level of the COMC. Fermat’s Little Theorem is one of the tools for establishing that certain expressions are always divisible by a prime. See our COMC math contest guide.

Canadian Mathematical Olympiad: Number theory at the CMO level regularly involves modular arithmetic arguments where Fermat’s Little Theorem is a key step. CMO number theory problems often require combining Fermat’s Little Theorem with other results (Wilson’s Theorem, the Chinese Remainder Theorem, properties of prime factorisation) to establish a deeper result. See our Canadian Mathematical Olympiad guide.

AMC 10/12:The AMC 10 and AMC 12 include modular arithmetic problems where knowing Fermat’s Little Theorem — or its consequence that ap11a^{p-1} \equiv 1ap−1≡1 — allows rapid identification of the remainder without computation.

For the full competition landscape, see our math competitions in Canada guide.


fermat's little theorem cta

Knowing Fermat’s Little Theorem is only one part of Euclid preparation. Students must recognise when to apply it, reduce large exponents accurately and combine it with other number-theory techniques. A free assessment identifies the skills they have mastered and what to develop next.

Assess their competition math skills →


Practice Problems

Set A — Direct application

Find each remainder:

  1. 250(mod7)2^{50} \pmod{7}250(mod7)
  2. 3100(mod11)3^{100} \pmod{11}3100(mod11)
  3. 430(mod13)4^{30} \pmod{13}430(mod13)
  4. 5999(mod7)5^{999} \pmod{7}5999(mod7)
  5. 61000(mod7)6^{1000} \pmod{7}61000(mod7)
  6. 10100(mod11)10^{100} \pmod{11}10100(mod11)

Set B — Mixed modular arithmetic

  1. What is the units digit of 71007^{100}7100? (Work modulo 5 and modulo 2.)
  2. Show that 51155^{11} – 5511−5 is divisible by 11.
  3. If ppp is prime and pap \nmid ap∤a, show that a2p21(modp)a^{2p-2} \equiv 1 \pmod{p}a2p−2≡1(modp).
  4. Find the remainder when 22024+320242^{2024} + 3^{2024}22024+32024 is divided by 5.

Set C — Competition level

  1. Prove that for any prime ppp and integer aaa: papap \mid a^p – ap∣ap−a.
  2. Show that n7nn^7 – nn7−n is divisible by 42 for every integer nnn.
  3. Find all primes ppp such that p2p+1p \mid 2^p + 1p∣2p+1.

Answers:

Set A:

  1. 261(mod7)2^6 \equiv 1 \pmod 726≡1(mod7); 50=8×6+250 = 8 \times 6 + 250=8×6+2; 25022=4(mod7)2^{50} \equiv 2^2 = 4 \pmod 7250≡22=4(mod7). Answer: 4
  2. 3101(mod11)3^{10} \equiv 1 \pmod{11}310≡1(mod11); 100=10×10100 = 10 \times 10100=10×10; 31001(mod11)3^{100} \equiv 1 \pmod{11}3100≡1(mod11). Answer: 1
  3. 4121(mod13)4^{12} \equiv 1 \pmod{13}412≡1(mod13); 30=2×12+630 = 2 \times 12 + 630=2×12+6; 43046=(42)3=16333=271(mod13)4^{30} \equiv 4^6 = (4^2)^3 = 16^3 \equiv 3^3 = 27 \equiv 1 \pmod{13}430≡46=(42)3=163≡33=27≡1(mod13). Answer: 1
  4. 561(mod7)5^6 \equiv 1 \pmod 756≡1(mod7); 999=166×6+3999 = 166 \times 6 + 3999=166×6+3; 599953=125=17×7+66(mod7)5^{999} \equiv 5^3 = 125 = 17 \times 7 + 6 \equiv 6 \pmod 75999≡53=125=17×7+6≡6(mod7). Answer: 6
  5. 61(mod7)6 \equiv -1 \pmod 76≡−1(mod7); 61000(1)1000=1(mod7)6^{1000} \equiv (-1)^{1000} = 1 \pmod 761000≡(−1)1000=1(mod7). Answer: 1
  6. 101(mod11)10 \equiv -1 \pmod{11}10≡−1(mod11); 10100(1)100=1(mod11)10^{100} \equiv (-1)^{100} = 1 \pmod{11}10100≡(−1)100=1(mod11). Answer: 1

Set B: 7. Units digit of 71007^{100}7100: 741(mod10)7^4 \equiv 1 \pmod{10}74≡1(mod10) (pattern: 7,9,3,1,7,…); 100=25×4100 = 25 \times 4100=25×4; units digit = 1. (Or: 742(mod5)7^4 \equiv 2 \pmod 574≡2(mod5) — actually 74=24011(mod5)7^4 = 2401 \equiv 1 \pmod 574=2401≡1(mod5), and 71001(mod2)7^{100} \equiv 1 \pmod 27100≡1(mod2), so 71001(mod10)7^{100} \equiv 1 \pmod{10}7100≡1(mod10).) 8. p=11p = 11p=11, a=5a = 5a=5: by Fermat’s Little Theorem 5115(mod11)5^{11} \equiv 5 \pmod{11}511≡5(mod11), so 11511511 \mid 5^{11} – 511∣511−5. ✓ 9. a2p2=(ap1)212=1(modp)a^{2p-2} = (a^{p-1})^2 \equiv 1^2 = 1 \pmod pa2p−2=(ap−1)2≡12=1(modp) by Fermat’s Little Theorem. ✓ 10. 241(mod5)2^4 \equiv 1 \pmod 524≡1(mod5) (period 4); 2024=506×42024 = 506 \times 42024=506×4; 220241(mod5)2^{2024} \equiv 1 \pmod 522024≡1(mod5). 341(mod5)3^4 \equiv 1 \pmod 534≡1(mod5) (period 4); 320241(mod5)3^{2024} \equiv 1 \pmod 532024≡1(mod5). Sum 2(mod5)\equiv 2 \pmod 5≡2(mod5). Answer: 2

Set C: 11. By Fermat’s Little Theorem: apa(modp)a^p \equiv a \pmod pap≡a(modp) for all integers aaa (the general form). Therefore papap \mid a^p – ap∣ap−a. ✓ (For pap \mid ap∣a: a0(modp)a \equiv 0 \pmod pa≡0(modp), so ap0a(modp)a^p \equiv 0 \equiv a \pmod pap≡0≡a(modp).) 12. n7n=n(n61)=n(n21)(n4+n2+1)n^7 – n = n(n^6 – 1) = n(n^2-1)(n^4+n^2+1)n7−n=n(n6−1)=n(n2−1)(n4+n2+1). By Fermat: n7n(mod7)n^7 \equiv n \pmod 7n7≡n(mod7) (so 7n7n7 \mid n^7 – n7∣n7−n); n3n(mod3)n^3 \equiv n \pmod 3n3≡n(mod3) (so 3n3nn7n3 \mid n^3 – n \mid n^7 – n3∣n3−n∣n7−n); n2n(mod2)n^2 \equiv n \pmod 2n2≡n(mod2) (so 2n2nn7n2 \mid n^2 – n \mid n^7 – n2∣n2−n∣n7−n). Since gcd(2,3,7)=1\gcd(2,3,7) = 1gcd(2,3,7)=1 pairwise, 42n7n42 \mid n^7 – n42∣n7−n. ✓ 13. By Fermat: 2p2(modp)2^p \equiv 2 \pmod p2p≡2(modp), so p2p+1p2+1=3p \mid 2^p + 1 \Rightarrow p \mid 2 + 1 = 3p∣2p+1⇒p∣2+1=3. So p=3p = 3p=3. Check: 23+1=9=3×32^3 + 1 = 9 = 3 \times 323+1=9=3×3. ✓ Answer: p=3p = 3 p=3.


Frequently Asked Questions

What does Fermat’s Little Theorem state?For any prime ppp and integer aaa not divisible by ppp: ap11(modp)a^{p-1} \equiv 1 \pmod{p}ap−1≡1(modp). Equivalently, apa(modp)a^p \equiv a \pmod{p}ap≡a(modp) for any integer aaa and prime ppp.

Why is it called “Little”?To distinguish it from Fermat’s Last Theorem (an+bn=cna^n + b^n = c^nan+bn=cn has no positive integer solutions for n3n \geq 3n≥3). The “little” theorem is the one about primes and modular arithmetic — more immediately useful in competition mathematics.

How do you use Fermat’s Little Theorem to find large remainders?Write the exponent nnn as n=q(p1)+rn = q(p-1) + rn=q(p−1)+r. Then an=(ap1)qar1qar=ar(modp)a^n = (a^{p-1})^q \cdot a^r \equiv 1^q \cdot a^r = a^r \pmod{p}an=(ap−1)q⋅ar≡1q⋅ar=ar(modp). Compute ara^rar directly since r<p1r < p-1r<p−1.

Does Fermat’s Little Theorem work for non-prime moduli?No — not directly. For non-prime moduli, Euler’s generalisation applies: aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}aϕ(n)≡1(modn) when gcd(a,n)=1\gcd(a,n) = 1gcd(a,n)=1, where ϕ(n)\phi(n)ϕ(n) is Euler’s totient function.

What are Carmichael numbers?Composite numbers nnn that satisfy an11(modn)a^{n-1} \equiv 1 \pmod{n}an−1≡1(modn) for all gcd(a,n)=1\gcd(a,n) = 1gcd(a,n)=1 — making them pass the Fermat primality test despite being composite. The smallest is 561. They do not violate Fermat’s Little Theorem (which requires nnn to be prime) but do limit the usefulness of the Fermat primality test.

Does Fermat’s Little Theorem appear in the Euclid Contest? Yes. Number theory problems involving large powers modulo a prime appear regularly in the Euclid, and Fermat’s Little Theorem is the standard tool for solving them efficiently.


See our related guides: Canadian Mathematical Olympiad guide · Euclid math contest guide · Euclid past contests guide · COMC math contest guide · math induction proof guide · proof by contradiction guide · contrapositive math guide · pigeonhole principle guide · Goldbach conjecture guide · math competitions in Canada


Fermat’s Little Theorem turns impossible calculations into one-line solutions. Know it cold before you need it.

Advanced number theory becomes manageable when students learn to see the structure behind each problem. In a free online trial lesson, your child can experience how Think Academy develops efficient, competition-level problem-solving strategies.

Book a free online trial lesson →

Leave a Reply

Your email address will not be published. Required fields are marked *