Part of my series explaining the RSA Algorithm
In 1640 Pierre de Fermat wrote in a letter to a friend.
If p is a prime and a is any integer not divisible by p, then aᵖ⁻¹ − 1 is divisible by p.
Unlike most of the things I say to my friends, Fermat was right. Fermat never proved it though. He left the job to Leonhard Euler, known here as the busiest person in all of mathematical history. In 1736 Euler showed that Fermat had been right, and went on to extend the claim too.

Fermat had bigger fish to fry, so this was his little theorem. It states that for any prime, p and integer a which is coprime to p: aᵖ⁻¹ % p = 1.
Why was Fermat right?
The list of numbers a, 2a, 3a, …, (p-1)a are all coprime to p, because p is prime. If we mod p each of these numbers we are guaranteed to produce a list of all the numbers between 1 & p-1. This is because there will be p - 1 distinct results. We can prove that they will be distinct.
> if `k` & `m` are two values between `1` & `p - 1`
> to prove they are NOT distinct we would want to find
ka ≡ ma (mod p)
> because of the cancellation rule
k ≡ m (mod p)
> because `k` & `m` were defined as being less than `p`
k = m
(For a reminder of the cancellation rule, take a look at rule 2 in our explanation of 4 Important Rules of Modular Arithmetic)
So we have shown that given the list a, 2a, 3a, …, (p-1)a (mod p), we would get the full set of numbers 1 -> p - 1. Ordering those results would give us the set: 1, 2, …, p-1.
An example might make things clearer:
p = 7, a = 3
> a, 2a, 3a, ..., (p-1)a
1(3), 2(3), 3(3), 4(3), 5(3), 6(3)
3, 6, 9, 12, 15, 18
> all `mod p`
3, 6, 2, 5, 1, 4
> ordering the results
1, 2, 3, 4, 5, 6
With this understanding we can prove Fermat’s Little Theorem.
> we know that
a, 2a, ..., (p-1)a ≡ 1, 2, ..., p-1 (mod p)
> multiplying all elements
a x 2a x ... x (p-1)a ≡ 1 x 2 x ... x (p-1) (mod p)
> simplifies
aᵖ⁻¹(p-1)! ≡ (p-1)! (mod p)
> cancel `(p-1)!`
> giving us
aᵖ⁻¹ ≡ 1 (mod p)
We’ve derived Fermat’s Little Theorem. This works for any positive integer a and prime p.
Having proved Fermat’s Little Theorem, Euler decided to keep going. He was able to show that for any two coprime integers, neither necessarily coprime: aᵠ⁽ⁿ⁾ ≡ 1 (mod n). 𝛗 refers to Euler’s Totient Function, which we explored previously. The Totient function of any integer n, returns the number of n’s coprimes smaller than n.
Unlike Fermat’s Little Theorem, Euler’s extension removed the need for the modulus to be a prime number. Thankfully though, we’ve done most of the work to prove this already.
Remember how, with Fermat’s Little Theorem, all of a’s multiples (mod p), gave the numbers 1 -> p-1. That list of numbers, 1 -> p-1 were p’s coprimes, given that p is prime. In this case the modulus n is not prime, but the list of n’s coprimes multiplied by a, all (mod n), still produces n’s coprimes. For example
> remembering that
𝛗(n) = Number of n's coprimes
n = 10
> n's coprimes
1, 3, 7, 9
a x 3a x 7a x 9a ≡ 1 x 3 x 7 x 9 (mod n)
> where n = 10, this gives us the number of "a" factors
aᵠ⁽¹⁰⁾
aᵠ⁽¹⁰⁾ x (1 x 3 x 7 x 9) ≡ (1 x 3 x 7 x 9) (mod n)
> cancel the coprime multiplication
aᵠ⁽¹⁰⁾ ≡ 1 (mod n)
Fermat’s Little Theorem & Euler’s Theorem are very important to Number Theory. They help solve cases of the Carmichael Function, which we covered in depth. They are interesting in isolation too, as an example use of Euler’s Totient Function.
And it all began with a letter to a friend almost 400 years ago.
HRV & Me: Taming a messy stressy mind - [Mar 8, 2026]
Resonance - [Feb 8, 2026]
Where Angels Fear to Tread -- EM Forster - [Jul 13, 2025]
Steve Jobs -- Walter Isaacson - [Jul 10, 2025]
The Fifth Risk -- Michael Lewis - [Jul 10, 2025]
The Ride of a Lifetime -- Bob Iger - [Jul 10, 2025]
James -- Percival Everett - [Jul 3, 2025]
Great Expectations -- Charles Dickens - [Jul 1, 2025]
Hillbilly Elegy -- JD Vance - [Jun 23, 2025]
Principles - [Jun 10, 2025]
Revenge of the Tipping Point -- Malcolm Gladwell - [Jun 9, 2025]
The Grand Babylon Hotel -- Arnold Bennett - [Jun 6, 2025]
The Seven Husbands of Evelyn Hugo -- Taylor Jenkins Reid - [Jun 4, 2025]
Rebecca -- Daphne du Maurier - [Jun 3, 2025]
A Promised Land - Barack Obama - [May 29, 2025]
Less - Andrew Sean Greer - [May 29, 2025]
Careless People - Sarah Wynn-Williams - [May 13, 2025]
Looking Glass War - John Le Carre - [May 7, 2025]
A Murder of Quality - John Le Carre - [May 4, 2025]
London Marathon 2025: Training Retrospective - [May 1, 2025]
The Human Factor - Graham Greene - [Apr 29, 2025]
London Marathon 2025: Race Review - [Apr 28, 2025]
Photos: London Marathon 2025 - [Apr 27, 2025]
Spectating the London Marathon 2025 [Sunday 27th April] - [Apr 27, 2025]
London Marathon 2025: Week 16 - [Apr 26, 2025]
Call for the Dead - John Le Carre - [Apr 23, 2025]
London Marathon 2025: Week 15 - [Apr 21, 2025]
The Manchurian Candidate - Richard Condon - [Apr 16, 2025]
London Marathon 2025: Week 14 - [Apr 13, 2025]
London Marathon 2025: Week 13 - [Apr 5, 2025]
London Marathon 2025: Week 12 - [Mar 30, 2025]
Effortless - Greg Mckeown - [Mar 26, 2025]
Leading - [Mar 26, 2025]
London Marathon 2025: Week 11 - [Mar 23, 2025]
London Marathon 2025: Week 10 - [Mar 16, 2025]
London Marathon 2025: Week 9 - [Mar 9, 2025]
London Marathon 2025: Week 8 - [Mar 2, 2025]
London Marathon 2025: Week 7 - [Feb 22, 2025]
London Marathon 2025: Week 6 - [Feb 16, 2025]
Problems & [Meta] Problem Solving - [Feb 16, 2025]
Little Dribbling - Bill Bryson - [Feb 14, 2025]
Bring Up the Bodies - Hilary Mantel - [Feb 10, 2025]
London Marathon 2025: Week 5 - [Feb 9, 2025]
Three Zero - [Feb 9, 2025]
The iPad mini has genuinely changed my life [no hyperbole] - [Feb 3, 2025]
London Marathon 2025: Week 4 - [Feb 2, 2025]
Coming AI: Valuing Humans in a world where they have no economic value - [Jan 28, 2025]
Value & Price - [Jan 28, 2025]
The Vegetarian - Han Kang - [Jan 27, 2025]
Wolf Hall - Hilary Mantel - [Jan 27, 2025]
London Marathon 2025: Week 3 - [Jan 26, 2025]
Deriving my own proof for Unitary matrices - [Jan 19, 2025]
London Marathon 2025: Week 2 - [Jan 19, 2025]
David Copperfield - Charles Dickens - [Jan 17, 2025]
London Marathon 2025: Week 1 - [Jan 12, 2025]
NYC & DC '24 - [Jan 9, 2025]
Linear Algebra Playground - [Jan 8, 2025]
Configuring an IKEA wireless light switch: Saving you the pain - [Jan 7, 2025]
Goals & Goal-setting - [Jan 7, 2025]
Organisation - [Jan 7, 2025]
Digital Feeds - [Jan 6, 2025]
London Marathon 2025: Training Begins - [Jan 5, 2025]
Everything I've read in 2025 (so far) - [Jan 1, 2025]