Mathwords logoMathwords

Fermat's Little Theorem — Definition, Formula & Examples

Fermat's Little Theorem states that if pp is a prime number and aa is any integer not divisible by pp, then ap1a^{p-1} leaves a remainder of 1 when divided by pp.

Let pp be a prime and aZa \in \mathbb{Z} with gcd(a,p)=1\gcd(a, p) = 1. Then ap11(modp)a^{p-1} \equiv 1 \pmod{p}. An equivalent formulation, valid for all integers aa (including multiples of pp), is apa(modp)a^{p} \equiv a \pmod{p}.

Key Formula

ap11(modp)a^{p-1} \equiv 1 \pmod{p}
Where:
  • aa = Any integer not divisible by $p$
  • pp = A prime number

How It Works

The theorem gives you a shortcut for computing large powers modulo a prime. Instead of calculating anmodpa^n \bmod p directly, you reduce the exponent nn modulo p1p - 1, since ap11a^{p-1} \equiv 1. Specifically, write n=q(p1)+rn = q(p-1) + r; then anar(modp)a^n \equiv a^r \pmod{p}. This dramatically shrinks the computation. The theorem also underpins primality testing algorithms and the RSA cryptosystem.

Worked Example

Problem: Compute 3100mod73^{100} \bmod 7.
Step 1: Since 7 is prime and 3 is not divisible by 7, Fermat's Little Theorem gives us:
361(mod7)3^{6} \equiv 1 \pmod{7}
Step 2: Reduce the exponent 100 modulo 6 (since p1=6p - 1 = 6):
100=16×6+4,so 1004(mod6)100 = 16 \times 6 + 4, \quad \text{so } 100 \equiv 4 \pmod{6}
Step 3: Replace the exponent with the remainder and compute:
310034=818111×7=8177=4(mod7)3^{100} \equiv 3^{4} = 81 \equiv 81 - 11 \times 7 = 81 - 77 = 4 \pmod{7}
Answer: 31004(mod7)3^{100} \equiv 4 \pmod{7}

Why It Matters

Fermat's Little Theorem is foundational to RSA encryption, where messages are encoded and decoded using modular exponentiation with large primes. It also forms the basis of the Fermat primality test, a fast probabilistic method for checking whether a number is prime. In an abstract algebra course, it appears as a special case of Lagrange's theorem applied to the multiplicative group Zp\mathbb{Z}_p^*.

Common Mistakes

Mistake: Applying the theorem when the modulus is not prime (e.g., using a81(mod9)a^{8} \equiv 1 \pmod{9}).
Correction: Fermat's Little Theorem requires pp to be prime. For composite moduli, use Euler's theorem with the Euler totient function ϕ(n)\phi(n) instead.