Classical results describing how powers behave modulo a prime or a general integer.
IntuitionA clock with a prime number of hours
Pick any hour hand position a on a 13-hour clock that isn't exactly at 12 (i.e. gcd(a,13)=1), and keep multiplying it by itself: a,a2,a3,…(mod13). Something remarkable happens after exactly 12 multiplications — you always land back on 1, no matter which starting hour you picked. This is Fermat's little theorem: for a prime p, every nonzero residue raised to the power p−1 returns to 1. Euler's theorem generalizes this from prime clocks to clocks of any size n, replacing p−1 by φ(n), the count of numbers up to n that share no factor with it. Together these two theorems are the mathematical engine inside RSA encryption and fast primality tests.
A network graph showing cycles formed by repeated multiplication modulo a prime.
For a prime modulus m=p and a coprime to p, the multiplication map x↦axmodp is a bijection of the nonzero residues — the key step in proving ap−1≡1(modp).
SchoolStatement and Euler's totient function
Definition: Euler's totient function φ(n)
For a positive integer n, define φ(n)=#{1≤k≤n:gcd(k,n)=1}: the number of integers from 1 to n that are coprime to n. For a prime p, every one of 1,…,p−1 is coprime to p, so φ(p)=p−1. In general φ can be computed from the prime factorization of n by φ(n)=n∏p∣n(1−p1), where the product runs over the distinct primes dividing n.
ap−1≡1(modp)(p prime,gcd(a,p)=1)
This is Fermat's little theorem. Euler's theorem replaces the prime modulus by any modulus n and p−1 by φ(n): whenever gcd(a,n)=1, we have aφ(n)≡1(modn). Setting n=p prime recovers Fermat's statement exactly, since φ(p)=p−1.
If p is prime and gcd(a,p)=1, then ap−1≡1(modp). Equivalently, ap≡a(modp) for every integer a.
Why is it true?
The nonzero residues modulo a prime p form a closed system under multiplication: multiplying every one of them by a fixed a just shuffles them among themselves. Repeating this shuffle p−1 times must therefore return every element to its own position, which is exactly the statement ap−1≡1(modp).
Proof
Consider the set S={1,2,…,p−1} of nonzero residues modulo p, and the map x↦axmodp sending each element of S to a residue.
This map is injective on S: if ax≡ay(modp) for x,y∈S, then since gcd(a,p)=1, the cancellation law (proved in the previous topic) gives x≡y(modp), hence x=y because both lie in {1,…,p−1}. Also no ax can be ≡0(modp) for x∈S, since p is prime and divides neither a nor x. So the map actually lands back inside S.
An injective map from the finite set S to itself is automatically a bijection. Therefore {a⋅1,a⋅2,…,a⋅(p−1)}(modp) is simply {1,2,…,p−1} written in a different order.
Multiply all p−1 elements of both sets together. On one side we get (a⋅1)(a⋅2)⋯(a⋅(p−1))=ap−1(p−1)!; on the other, since it's the same numbers reordered, we get exactly (p−1)!. So ap−1(p−1)!≡(p−1)!(modp).
Since p is prime, none of 1,2,…,p−1 is divisible by p, so gcd((p−1)!,p)=1. Applying the cancellation law once more to remove the common factor (p−1)! from both sides gives ap−1≡1(modp). ■
(Group-theoretic restatement: the nonzero residues form a group of order p−1 under multiplication since p is prime, and Lagrange's theorem says the order of any element — here, the smallest k with ak≡1 — must divide the group's order p−1; hence ap−1≡1(modp) automatically.)
If gcd(a,n)=1, then aφ(n)≡1(modn), where φ is Euler's totient function.
Why is it true?
This is exactly Fermat's argument with a general modulus n: instead of all nonzero residues (which only form a closed multiplicative system when n is prime), we restrict to the residues actually coprime to n — the ones that have inverses — and the same shuffling argument applies to that smaller, closed set of size φ(n).
Proof
Let R={r1,…,rφ(n)} be a reduced residue system modulo n: representatives of every residue class coprime to n.
Multiplication by a permutes R: for each ri, gcd(ari,n)=1 because gcd(a,n)=1 and gcd(ri,n)=1 (a product is coprime to n iff both factors are), so arimodn is again a member of the coprime residue classes; and the map ri↦arimodn is injective by the cancellation law, since gcd(a,n)=1.
An injective map from the finite set R to itself (up to residue) is a bijection, so {ar1,…,arφ(n)}(modn) is just {r1,…,rφ(n)} reordered.
Multiplying all φ(n) elements of each set: aφ(n)(r1r2⋯rφ(n))≡r1r2⋯rφ(n)(modn). Since each ri is coprime to n, so is their product R=∏ri, i.e. gcd(R,n)=1.
Cancel R from both sides (valid since gcd(R,n)=1) to get aφ(n)≡1(modn). ■
This is again Lagrange's theorem in disguise: the residues coprime to n form a group of order φ(n) under multiplication (the group of units (Z/nZ)∗), and the order of any element divides φ(n).
AdvancedReal-World Applications and Worked Examples
Euler's theorem is the mathematical heart of RSA public-key cryptography — decryption correctness relies on raising to the power φ(n) and getting back to 1. Fermat's little theorem gives a fast probabilistic primality test (try a base a and check ap−1≡1), and lets one compute a modular inverse as ap−2modp without the extended Euclidean algorithm. Both theorems also underlie fast modular exponentiation used throughout coding theory and pseudorandom number generation.
Example: Why RSA decryption works
Take p=5,q=11, so n=pq=55 and φ(n)=(p−1)(q−1)=40. Choose public exponent e=3 (since gcd(3,40)=1) and private exponent d=27 (since 3×27=81=2×40+1, so ed≡1(mod40)). Encrypt m=2 as c=memodn, then decrypt c and check you recover m=2.
Solution
Encryption: c=23mod55=8.
Decryption raises the ciphertext to the private exponent: m′=cdmodn=827mod55. Because ed=1+kφ(n) for some integer k (here 81=1+2×40), we have m′≡med=m1+kφ(n)=m⋅(mφ(n))k(modn).
Since gcd(m,n)=gcd(2,55)=1, Euler's theorem gives mφ(n)≡1(modn), so (mφ(n))k≡1k=1(modn), and therefore m′≡m⋅1=m(modn).
Numerically: 827mod55 can be computed by repeated squaring (82=64≡9, 84≡81≡26, 88≡262=676≡676−660=16, 816≡162=256≡256−220=36; then 827=816⋅88⋅82⋅81≡36×16×9×8). Reducing step by step modulo 55 gives exactly 2, confirming that decryption recovers the original message m=2 — precisely as Euler's theorem guarantees for any message and any two primes p,q, not just this example.
Example: A Fermat pseudoprime: when the test lies
Show that 341=11×31 (which is composite) still satisfies 2340≡1(mod341) — the exact conclusion Fermat's little theorem would predict if 341 were prime.
Solution
Work modulo each prime factor separately. Modulo 11: since 11 is prime and gcd(2,11)=1, Fermat gives 210≡1(mod11); since 340=10×34, we get 2340=(210)34≡134=1(mod11).
Modulo 31: here 25=32≡1(mod31), so 2 has order 5 modulo 31 (a divisor of φ(31)=30, consistent with Fermat/Euler). Since 340=5×68 is a multiple of 5, 2340=(25)68≡168=1(mod31).
So 2340≡1 both modulo 11 and modulo 31. By the Chinese remainder theorem (the next topic), being ≡1 modulo two coprime numbers 11 and 31 forces 2340≡1(mod341) as well, even though 341 is not prime.
A composite number n that passes an−1≡1(modn) for some base a coprime to it is called a **Fermat pseudoprime to base a**; 341 is the smallest pseudoprime to base 2. This is exactly why the Fermat test is only a probabilistic heuristic, not a proof of primality — a pitfall explored further below.
ResearchFermat and Euler at the research frontier: primality, pseudoprimes, and post-quantum cryptography
By Fermat's little theorem, if p=13 and gcd(a,13)=1, what is a12(mod13)?
Compute φ(20).
In RSA with n=55, φ(n)=40, and public exponent e=3, the private exponent d must satisfy:
341=11×31 satisfies 2340≡1(mod341) despite being composite. What is this phenomenon called?