Quant Memo
Core

Fermat's Little Theorem in Puzzles

Fermat's little theorem says that for a prime p, any number raised to the p-1 power leaves remainder 1 when divided by p (unless it's a multiple of p) — a one-line fact that collapses huge modular-power interview questions into small arithmetic.

Prerequisites: Finding the Last Digit of a Huge Power

What is the remainder when 21002^{100} is divided by 101? Computing 21002^{100} directly gives a 31-digit number — not something to do by hand in an interview. But 101 is prime, and that single fact makes the problem almost trivial once you know Fermat's little theorem.

The core idea: a prime modulus forces powers to cycle back to 1

Fermat's little theorem states: if pp is a prime number and aa is any integer not divisible by pp, then

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

In plain English: raise aa to the power one less than the prime, divide by the prime, and the remainder is always exactly 1 — no matter which valid aa or which prime pp you pick. This holds because the nonzero remainders 1,2,,p11, 2, \dots, p-1 under multiplication modulo pp form a tightly structured group (every nonzero remainder has a multiplicative "partner" that undoes it), and a foundational fact about that structure — closely related to counting how repeated multiplication permutes the remainders — forces every element's (p1)(p-1)-th power back to 1.

Worked example 1: 2100mod1012^{100} \bmod 101

Since 101 is prime and 22 isn't a multiple of 101, Fermat's little theorem gives 21001(mod101)2^{100} \equiv 1 \pmod{101} directly — the exponent 100 is exactly p1p - 1 for p=101p=101, so no further computation is needed. The remainder is 1.

Worked example 2: an exponent that isn't exactly p1p-1

What is 3206mod73^{206} \bmod 7? Here p=7p=7 is prime, so Fermat's little theorem says 361(mod7)3^6 \equiv 1 \pmod 7. The exponent 206 isn't 66, but you can use the theorem to reduce it: since 3613^6 \equiv 1, any multiple of 6 in the exponent also gives 1, so 3206=36×34+2=(36)34×32134×32=9(mod7)3^{206} = 3^{6 \times 34 + 2} = (3^6)^{34} \times 3^2 \equiv 1^{34} \times 3^2 = 9 \pmod 7. And 9mod7=29 \bmod 7 = 2. So 32062(mod7)3^{206} \equiv 2 \pmod 7. The technique generalizes: to compute anmodpa^n \bmod p for prime pp, first reduce the exponent nn modulo p1p-1 (using Fermat's little theorem to collapse full cycles of p1p-1 down to 1), then compute the much smaller resulting power directly.

3^206 mod 7, using 3^6 = 1 (mod 7) 3^6 repeated 34 times -> collapses to 1 3^2 206 = 6 x 34 + 2, so answer = 3^2 mod 7 = 2
Every full cycle of length p-1 in the exponent collapses to 1 by Fermat's little theorem, leaving only the small remainder power to compute.

For a prime pp and aa not divisible by pp: ap11(modp)a^{p-1} \equiv 1 \pmod p. To compute anmodpa^n \bmod p for a large nn, reduce the exponent modulo p1p-1 first, then compute the small remaining power — never compute the huge power directly.

What this means in practice

This is the number-theoretic engine behind RSA encryption and most public-key cryptography, where huge modular exponentiations need to be both computable and provably hard to reverse without the right key. In interviews, the pattern-recognition step — noticing that a modulus is prime and that the question is really asking for a modular power — is the real skill being tested, since the theorem itself is a one-line lookup once you spot it applies.

Fermat's little theorem requires pp to be prime and aa to not be a multiple of pp. Applying it when the modulus is composite (not prime) gives wrong answers — that case needs the more general Euler's theorem, which replaces p1p-1 with Euler's totient function ϕ(n)\phi(n) counting numbers coprime to nn.

Related concepts

Practice in interviews

Further reading

  • Niven, Zuckerman & Montgomery, An Introduction to the Theory of Numbers, ch. 2
ShareTwitterLinkedIn