Skip to content

Fermat's Little Theorem

For prime p, every integer a satisfies a^p ≡ a modulo p; nonzero residues satisfy a^(p−1) ≡ 1.

Version
v1 · 2026-10-03 · History
Domain-specific #
13225
Aliases
Fermats Little Theorem, Fermat Theorem Mod P

Core Idea

For a prime p, Fermat's little theorem says a^p ≡ a (mod p) for every integer a. When a is not divisible by p, it also says a^(p−1) ≡ 1 (mod p). The second form uses the fact that a nonzero residue modulo a prime is invertible; it is false if a is zero modulo p. The p−1 exponent reflects the p−1 nonzero residue classes.[1][2]

How would you explain it like I'm…

The Prime Leftover Trick

Some numbers, like 5, are special 'prime' numbers that can't be split into equal groups except by ones. Pick a smaller number, like 2, and multiply 2 by itself five times to get 32. Now put 32 things into groups of 5: the leftover is 2, the same number you started with. Fermat's Little Theorem says this trick always works when you use a prime.

Prime Power Remainder Rule

A prime number is one that can only be divided evenly by 1 and itself, like 5 or 7. Fermat's Little Theorem says: if p is prime, and you take any whole number a, multiply it by itself p times, and then find the remainder after dividing by p, you get the same remainder as a itself. For example, 2×2×2×2×2 = 32, and 32 divided by 5 leaves 2, the number we started with. If a isn't a multiple of p, multiplying it by itself p−1 times always leaves remainder 1. The rule only promises this for prime p, so you can't use a pass on the test to prove a number is prime.

Prime-Forced Power Congruence

Fermat's Little Theorem states that for a prime p and any integer a, a^p ≡ a (mod p), meaning a^p and a leave the same remainder when divided by p. If p does not divide a, you can cancel one factor of a and get a^(p−1) ≡ 1 (mod p). Cancelling is allowed because every nonzero remainder mod a prime has a multiplicative inverse. If p does divide a, the first version still holds (0 ≡ 0), but the second would say 0 ≡ 1, which is false, so that condition matters. The theorem gives a one-way test: if some a fails a^(n−1) ≡ 1 mod n, then n is definitely not prime. But passing does not prove n is prime, because some composite numbers, like 561, pass for every base that shares no factor with them.

 

Fermat's Little Theorem: if p is prime, then a^p ≡ a (mod p) for every integer a, and a^(p−1) ≡ 1 (mod p) whenever p ∤ a. The second form follows from the first by cancelling a, valid because the nonzero residues mod p are all invertible; when p | a the second form fails, so the hypothesis is essential. Structurally, the nonzero residues mod p form a multiplicative group of order p−1, and by Lagrange's theorem every element's order divides p−1, forcing a^(p−1) = 1. This is the prime case of Euler's theorem and can also be derived from a reduced residue system. The theorem yields a compositeness test: a base a coprime to n with a^(n−1) ≢ 1 (mod n) proves n composite. The converse fails: pseudoprimes pass for particular bases, and Carmichael numbers such as 561 pass for all coprime bases. That is a limitation of reversing the theorem, not an exception to it.

Scope of Application

With a known prime modulus, the theorem simplifies powers and yields a^(p−2) as the inverse of a nonzero a. For an unknown candidate n, a coprime base that fails a^(n−1) ≡ 1 (mod n) proves n composite. A passing base does not prove primality: composite Carmichael numbers, including 561, can pass for every coprime base.[1][3]

Clarity

The logic runs from prime to congruence, not from congruence to prime. A failed necessary condition refutes primality, while a passed one is inconclusive. Also keep the two formulas' base conditions separate: at p=5, a=5, the all-integer form gives 5^5 ≡ 5 ≡ 0, but 5^4 ≡ 0, not one.[1][3]

Manages Complexity

For a nonzero residue modulo a known prime, exponents can be simplified modulo p−1 rather than expanded term by term. The finite unit-group explanation compresses many calculations into one rule. The compression remains safe only while “known prime” and “nonzero base” stay attached to it.[1]

Abstract Reasoning

First establish whether the modulus is prime and whether the base is a unit. If both hold, use a^(p−1) ≡ 1 to simplify powers or derive an inverse; if the base is zero, use only a^p ≡ a. If testing a candidate rather than working under a proven prime premise, treat a failing congruence as a compositeness witness and a pass as unresolved. This avoids a false primality certificate.[1][3]

Knowledge Transfer

The same theorem applies across prime-residue calculations, inverse reasoning and one-way primality screening. Euler's theorem generalizes the unit-group pattern with exponent φ(n) for broader moduli, but that does not license blindly replacing p by a composite in the p−1 formula.

Relationships to Other Abstractions

Local relationship map for Fermat's Little TheoremParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Fermat'sLittle TheoremDOMAINDomain-specific abstraction: Modular arithmetic — presupposesModulararithmeticDOMAIN

Current abstraction Fermat's Little Theorem Domain-specific

Parents (1) — outgoing typed relations

  • Fermat's Little Theorem presupposes Modular arithmetic Domain-specific

    A prime-modulus power congruence presupposes modular arithmetic and adds the theorem's prime and base conditions.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Fermat's Little Theorem sits in a moderately populated region (44th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.

Family — Number-Theoretic Properties & Tests (22 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] Jonathan A. Poritz, Yet Another Introductory Number Theory Textbook, §3.3, original authored open textbook rendered by Mathematics LibreTexts. registry ↩a ↩b ↩c ↩d ↩e

[2] Abhinav Kumar, MIT 18.781 Theory of Numbers, Lecture 4, Fermat corollary and proof. registry ↩

[3] Abhinav Kumar, MIT 18.781 Theory of Numbers, Lecture 6, primality testing and Carmichael limitation. registry ↩a ↩b ↩c