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 moreCarmichael 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesSet Operations: Union, Intersection, and ComplementProof by CasesProving by Cases and ExhaustionVacuous Truth and Trivial CasesProof by Cases (Proof by Exhaustion)Mathematical InductionDivisibility and Greatest Common DivisorThe Fundamental Theorem of ArithmeticDivisibility Theory (Formal Treatment)Fundamental Theorem of Arithmetic (Rigorous Proof)Arithmetic Functions and MultiplicativityDirichlet Series and L-FunctionsPrimes in Arithmetic Progressions (Dirichlet's Theorem)Distribution of PrimesIntroduction to the Riemann Zeta FunctionDirichlet Series and L-FunctionsPrimes in Arithmetic Progressions (Dirichlet's Theorem)Wilson's TheoremFermat's Little Theorem

Longest path: 87 steps · 395 total prerequisite topics

Prerequisites (3)

Leads To (3)