Folioby Interconnected
Log InSign Up

Fermat's little theorem and the hidden symmetry of remainders

From the group structure of (Z/pZ)* we derive Fermat's little theorem, explain why the set {a, 2a, ..., (p-1)a} is a permutation of {1, 2, ..., p-1}, show that a^{p-2} is the modular inverse, and generalize to Euler's theorem.

FO
Folio Official
March 1, 2026

Fermat's little theorem is one of the most frequently invoked results in number theory. The statement – if p is prime and gcd(a,p)=1, then ap−1≡1(modp)– is concise, but behind it lies a beautiful symmetry in the world of remainders.

1 The statement

Theorem 1 (Fermat's little theorem).
Let p be a prime and let a be an integer not divisible by p. Then
ap−1≡1(modp).
Remark 2.
An equivalent formulation: for any integer a, ap≡a(modp). When gcd(a,p)=1, divide both sides by a to recover the first form; when p∣a, both sides are congruent to 0.

2 The permutation argument: the heart of the proof

Proof.
Consider the set S={1,2,3,…,p−1}. We claim that when gcd(a,p)=1, the set
aS={a⋅1,a⋅2,…,a⋅(p−1)}(modp)
is a permutation of S– the same set of elements, merely rearranged.

Step 1: No element of aS is 0(modp). Indeed, p∣ai would require p∣a or p∣i (by Euclid's lemma), but gcd(a,p)=1 and 1≤i≤p−1, so neither holds.

Step 2: The elements of aS are pairwise distinct modulo p. If ai≡aj(modp) with i=j, then a(i−j)≡0(modp). Since gcd(a,p)=1, we get p∣(i−j), which is impossible for ∣i−j∣<p.

Therefore aS=S as sets modulo p. Multiplying all elements on each side:
i=1∏p−1​(ai)≡i=1∏p−1​i(modp).
The left side is ap−1⋅(p−1)! and the right side is (p−1)!. Since gcd((p−1)!,p)=1, we may cancel (p−1)! to obtain
ap−1≡1(modp).
□
Remark 3 (Intuition).
Multiplication by a shuffles the elements of {1,2,…,p−1}, but the set as a whole is unchanged. The total product – obtained by multiplying all elements together – is therefore an invariant, and from that invariant the congruence ap−1≡1 drops out.

3 The group(Z/pZ)∗

Fermat's little theorem admits a clean algebraic interpretation.

Definition 4 ((Z/pZ)∗).
For a prime p, the set (Z/pZ)∗={1,2,…,p−1} equipped with multiplication modulo p forms a group– the multiplicative group of integers modulo p.
Remark 5.
Let us verify the group axioms:
  • Closure: gcd(a,p)=1 and gcd(b,p)=1 imply gcd(ab,p)=1.

  • Associativity: inherited from integer multiplication.

  • Identity: 1.

  • Inverses: computed via the extended Euclidean algorithm (previous article).

Theorem 6 (Consequence of Lagrange's theorem).
The group (Z/pZ)∗ has order p−1. By Lagrange's theorem, the order of every element divides p−1, so ap−1=e=1.
Remark 7.
Fermat's little theorem is a special case of the general fact that in a finite group, the order of every element divides the order of the group. The elementary proof via the permutation argument is, at its core, the same reasoning that underlies the proof of Lagrange's theorem.

4 Whyap−2is the modular inverse

Theorem 8.
Let p be prime and gcd(a,p)=1. Then the modular inverse of a modulo p is ap−2.
Proof.
Fermat's little theorem gives ap−1≡1(modp). Rewriting this as a⋅ap−2≡1(modp), we see that ap−2 is the inverse of a. □
Example 9.
With p=7 and a=3: 3−1≡35=243≡243−34×7=243−238=5(mod7). Check: 3×5=15≡1(mod7).

5 Modular inverses in competitive programming

Remark 10 (Two methods compared).
There are two standard ways to compute the modular inverse modulo p:
  • Fermat's little theorem: compute ap−2modp by repeated squaring in O(logp).

  • Extended Euclidean algorithm: also O(logp), with a smaller constant.

When p is prime, either method works. When the modulus n is not prime, only the extended Euclidean algorithm applies (provided gcd(a,n)=1).
Example 11 (Binomial coefficients modulo p).
A classic problem asks for (kn​)modp. Since (kn​)=k!(n−k)!n!​, we need the modular inverses of k! and (n−k)!. With n!modp precomputed, both inverses can be obtained via Fermat's little theorem in O(logp).

6 Generalization: Euler's theorem

Definition 12 (Euler's totient function).
φ(n) is the number of integers in {1,2,…,n} that are coprime to n.
Theorem 13 (Euler's theorem).
If gcd(a,n)=1, then aφ(n)≡1(modn).
Remark 14.
When n=p is prime, φ(p)=p−1 and Euler's theorem reduces to Fermat's little theorem. Euler's theorem is the natural generalization: the group (Z/nZ)∗ has order φ(n), and the result follows from Lagrange's theorem applied to this group.

7 Takeaway

  • Fermat's little theorem: if p is prime and gcd(a,p)=1, then ap−1≡1(modp).

  • The proof rests on the observation that multiplication by a permutes the set {1,…,p−1}.

  • In the language of algebra, this is Lagrange's theorem applied to (Z/pZ)∗.

  • The identity a⋅ap−2≡1 is a direct consequence, giving the modular inverse.

  • The generalization is Euler's theorem, which provides the theoretical foundation for RSA encryption.

In the next article, we explore the "merging" of simultaneous congruences – the Chinese Remainder Theorem.

Number TheoryMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Number Theory — Between the LinesPart 4 of 8
Previous
Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says
Next
The Chinese Remainder Theorem: why simultaneous congruences can be merged

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Unique prime factorization is not as obvious as it seems. Through a counterexample in Z[sqrt(-5)], we witness uniqueness break down, then prove both the existence and uniqueness parts of the Fundamental Theorem, and survey the Sieve of Eratosthenes and the linear sieve.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

The Chinese Remainder Theorem: why simultaneous congruences can be merged

We view CRT as the number-theoretic incarnation of divide-and-conquer, prove the ring isomorphism Z/mnZ = Z/mZ x Z/nZ, and show how Garner's algorithm avoids overflow in practice.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

What does it mean to "divide evenly"? Why number theory begins with mod

The modular reduction map is a projection that deliberately discards information. Clocks, days of the week, and the competitive-programming modulus 10^9+7 all share the same algebraic structure. From the definition of divisibility through congruences to the ring Z/nZ, we uncover the reason number theory starts where it does.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

Why the Euclidean algorithm finds the greatest common divisor: from subtraction to division

The recursion gcd(a,b) = gcd(b, a mod b) hides an invariant: the set of common divisors never changes. Starting from a naive subtraction-based method and upgrading to division, we derive Bezout's identity, the extended Euclidean algorithm, and modular inverses.

Number TheoryMathematicsBetween the Lines