A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Fermat's Little Theorem

Graduate Depth 86 in the knowledge graph ☐ I know this ☆ Set as goal
16topics build on this
395prerequisites beneath it
See this on the map →
Modular Arithmetic and CongruencesGroup Definition and Examples+1 more→→Carmichael Function and Carmichael NumbersEuler's Criterion for Quadratic Residues+1 more
modular-arithmetic group-theory primes fermat

Core Idea

If p is prime and gcd(a, p) = 1, then ap−1 ≡ 1 (mod p). This theorem follows from Lagrange's theorem applied to the multiplicative group (ℤ/pℤ)* and forms the basis for Fermat primality tests and many cryptographic algorithms.

How It's Best Learned

Derive it from group theory using the multiplicative group mod p. Verify with numerical examples (e.g., 24 ≡ 1 (mod 5)). Apply it to compute large powers modulo p efficiently.

Common Misconceptions

The converse is false: an−1 ≡ 1 (mod n) does not imply n is prime (Carmichael numbers counterexample: 561 = 3·11·17). The condition gcd(a, p) = 1 is essential; the theorem fails when a is divisible by p.

Explainer

From your work with modular arithmetic, you know that integers mod p form a system where addition and multiplication wrap around. When p is prime, something special happens: every nonzero element has a multiplicative inverse mod p. This means the nonzero residues {1, 2, ..., p−1} form a multiplicative group under multiplication mod p, denoted (ℤ/pℤ)*. Its size — the order of the group — is p−1.

Fermat's Little Theorem follows almost immediately from one key fact about groups: for any element a in a finite group of order n, raising a to the power n gives the identity. In (ℤ/pℤ)*, the identity is 1 and the group has order p−1, so ap−1 ≡ 1 (mod p) for any a not divisible by p. If you haven't seen the group-theoretic proof yet, there's a more elementary argument: list the p−1 nonzero residues, then consider the list multiplied by a (mod p). Because a is coprime to p, the new list is a permutation of the old one. So the product of both lists must be equal mod p — the ap−1 factor cancels against the same product, leaving ap−1 ≡ 1.

A useful corollary restates the theorem as ap ≡ a (mod p) for *all* integers a (including multiples of p, where both sides are 0). This version is often more convenient in proofs. The practical power of the theorem is modular exponentiation: to compute 71000 (mod 13), note that 13 is prime and 7 is coprime to 13, so 712 ≡ 1 (mod 13). Write 1000 = 83 × 12 + 4, so 71000 = (712)83 · 74 ≡ 183 · 74 ≡ 2401 ≡ 9 (mod 13). A computation that seemed impossible becomes routine.

The one trap to watch: the converse fails. If an−1 ≡ 1 (mod n), you cannot conclude n is prime. Carmichael numbers like 561 = 3 · 11 · 17 satisfy this equation for all a coprime to n, even though they are composite. This is why Fermat's test is only a *probable* primality check — it identifies non-primes efficiently (if the equation fails, n is definitely composite), but passing the test is not a guarantee of primality. This gap is closed by more sophisticated tests like Miller–Rabin, which also underpins the RSA cryptographic system you will encounter next.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Proof by Cases → Proving by Cases and Exhaustion → Vacuous Truth and Trivial Cases → Proof by Cases (Proof by Exhaustion) → Mathematical Induction → Divisibility and Greatest Common Divisor → The Fundamental Theorem of Arithmetic → Divisibility Theory (Formal Treatment) → Fundamental Theorem of Arithmetic (Rigorous Proof) → Arithmetic Functions and Multiplicativity → Dirichlet Series and L-Functions → Primes in Arithmetic Progressions (Dirichlet's Theorem) → Distribution of Primes → Introduction to the Riemann Zeta Function → Dirichlet Series and L-Functions → Primes in Arithmetic Progressions (Dirichlet's Theorem) → Wilson's Theorem → Fermat's Little Theorem

Longest path: 87 steps · 395 total prerequisite topics

Prerequisites (3)

Leads To (3)