Skip to content
Part IA Michaelmas, Lent Term

Fermat's Little Theorem

Statement of Fermat’s Little Theorem

Theorem

Fermat’s Little Theorem: If pp is a prime and aa is an integer not divisible by pp, then:

ap11(modp)a^{p-1} \equiv 1 \pmod{p}

Equivalent Formulation

For any integer aa and prime pp:

apa(modp)a^p \equiv a \pmod{p}

This form works even when pap \mid a (both sides are 0modp0 \bmod p).

Examples

  • p=5p = 5, a=2a = 2: 24=161(mod5)2^4 = 16 \equiv 1 \pmod{5}
  • p=7p = 7, a=3a = 3: 36=7291(mod7)3^6 = 729 \equiv 1 \pmod{7}
  • p=11p = 11, a=2a = 2: 210=10241(mod11)2^{10} = 1024 \equiv 1 \pmod{11}

Examinable Proof

The Proof Strategy

We show that a,2a,3a,,(p1)aa, 2a, 3a, \ldots, (p-1)a modulo pp form a permutation of 1,2,3,,p11, 2, 3, \ldots, p-1.

Step 1: Non-zeroness

Each ia(modp)ia \pmod{p} is non-zero for 1ip11 \leq i \leq p-1.

Reason: pap \nmid a by hypothesis, and pip \nmid i since 1i<p1 \leq i < p and pp is prime.

Thus piap \nmid ia.

Step 2: Distinctness

If iaja(modp)ia \equiv ja \pmod{p} for 1i,jp11 \leq i, j \leq p-1, then i=ji = j.

Proof: iaja(modp)ia \equiv ja \pmod{p} means p(ij)ap \mid (i-j)a.

Since pap \nmid a and pp is prime, Euclid’s lemma gives p(ij)p \mid (i-j).

But ijp2<p|i - j| \leq p - 2 < p, so ij=0|i - j| = 0 and i=ji = j.

Step 3: Permutation Argument

The numbers 1,2,,p11, 2, \ldots, p-1 are p1p-1 distinct non-zero residues modulo pp.

By Steps 1 and 2, a,2a,,(p1)aa, 2a, \ldots, (p-1)a are also p1p-1 distinct non-zero residues modulo pp.

Therefore:

{a,2a,,(p1)a}={1,2,,p1}(modp)\{a, 2a, \ldots, (p-1)a\} = \{1, 2, \ldots, p-1\} \pmod{p}

Step 4: Product Equality

Taking products:

a2a3a(p1)a123(p1)(modp)a \cdot 2a \cdot 3a \cdot \cdots \cdot (p-1)a \equiv 1 \cdot 2 \cdot 3 \cdot \cdots \cdot (p-1) \pmod{p}

This simplifies to:

ap1(p1)!(p1)!(modp)a^{p-1} \cdot (p-1)! \equiv (p-1)! \pmod{p}

Step 5: Cancellation

Since 1(p1)!<p!1 \leq (p-1)! < p! and (p1)!(p-1)! is the product of integers 11 through p1p-1, none of which are divisible by pp, we have p(p1)!p \nmid (p-1)!.

Thus gcd(p,(p1)!)=1\gcd(p, (p-1)!) = 1.

By the cancellation property of modular arithmetic:

ap11(modp)a^{p-1} \equiv 1 \pmod{p}

Applications

Modular Exponentiation

To compute akmodpa^k \bmod p where pp is prime:

  1. Reduce kk modulo p1p-1: k=q(p1)+rk = q(p-1) + r with 0r<p10 \leq r < p-1
  2. Then akar(modp)a^k \equiv a^r \pmod{p}

Example: Compute 3100mod73^{100} \bmod 7.

100=166+4100 = 16 \cdot 6 + 4, so 310034=814(mod7)3^{100} \equiv 3^4 = 81 \equiv 4 \pmod{7}.

Primality Testing

If there exists aa with pap \nmid a such that ap1≢1(modp)a^{p-1} \not\equiv 1 \pmod{p}, then pp is not prime.

Note: The converse is not true in general. A Carmichael number is composite but satisfies an11(modn)a^{n-1} \equiv 1 \pmod{n} for all aa coprime to nn.

Diffie-Hellman Key Exchange

Two parties agree on:

  • A large prime pp
  • A generator gg of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times

Each picks a secret:

  • Alice picks secret aa, sends gamodpg^a \bmod p
  • Bob picks secret bb, sends gbmodpg^b \bmod p

Shared secret: (ga)b=(gb)a=gabmodp(g^a)^b = (g^b)^a = g^{ab} \bmod p.

Security relies on the difficulty of computing discrete logarithms.

Fermat’s Theorem and Wilson’s Theorem

Wilson’s Theorem

(p1)!1(modp)(p-1)! \equiv -1 \pmod{p} iff pp is prime.

Connection

In our proof of Fermat’s Little Theorem, we used that (p1)!(p-1)! is coprime to pp. Wilson’s theorem gives its exact value.

Generalisations

Euler’s Theorem

For aa coprime to nn: aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}, where ϕ(n)\phi(n) is Euler’s totient function.

When nn is prime, ϕ(n)=n1\phi(n) = n-1, giving Fermat’s Little Theorem.


Summary

  • Fermat’s Little Theorem: ap11(modp)a^{p-1} \equiv 1 \pmod{p} for prime pp and pap \nmid a
  • Proof uses permutation argument and cancellation
  • Applications: fast modular exponentiation, cryptography (Diffie-Hellman), primality testing
  • Generalised by Euler’s theorem to composite moduli