MathLabs

Arithmetic and number theory

Fermat's little theorem and Euler's theorem

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 aa on a 1313-hour clock that isn't exactly at 1212 (i.e. gcd⁡(a,13)=1\gcd(a,13)=1), and keep multiplying it by itself: a,a2,a3,…(mod13)a, a^2, a^3, \dots \pmod{13}. Something remarkable happens after exactly 1212 multiplications — you always land back on 11, no matter which starting hour you picked. This is Fermat's little theorem: for a prime pp, every nonzero residue raised to the power p−1p-1 returns to 11. Euler's theorem generalizes this from prime clocks to clocks of any size nn, replacing p−1p-1 by φ(n)\varphi(n), the count of numbers up to nn 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=pm = p and aa coprime to pp, the multiplication map x↦ax mod px \mapsto ax \bmod p is a bijection of the nonzero residues — the key step in proving ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p.

SchoolStatement and Euler's totient function

Definition: Euler's totient function φ(n)\varphi(n)

For a positive integer nn, define φ(n)=#{ 1≤k≤n:gcd⁡(k,n)=1 }\varphi(n) = \#\{\,1\le k\le n : \gcd(k,n)=1\,\}: the number of integers from 11 to nn that are coprime to nn. For a prime pp, every one of 1,…,p−11,\dots,p-1 is coprime to pp, so φ(p)=p−1\varphi(p)=p-1. In general φ\varphi can be computed from the prime factorization of nn by φ(n)=n∏p∣n(1−1p)\varphi(n) = n\prod_{p\mid n}\left(1-\frac{1}{p}\right), where the product runs over the distinct primes dividing nn.

ap−1≡1(modp)(p prime, gcd⁡(a,p)=1)a^{p-1} \equiv 1 \pmod{p} \qquad (p \text{ prime},\ \gcd(a,p)=1)

This is Fermat's little theorem. Euler's theorem replaces the prime modulus by any modulus nn and p−1p-1 by φ(n)\varphi(n): whenever gcd⁡(a,n)=1\gcd(a,n)=1, we have aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}. Setting n=pn=p prime recovers Fermat's statement exactly, since φ(p)=p−1\varphi(p)=p-1.

aφ(n)≡1(modn)(gcd⁡(a,n)=1)a^{\varphi(n)} \equiv 1 \pmod{n} \qquad (\gcd(a,n)=1)
Fermat vs. Euler
ModulusStatementUnderlying group
Prime ppap−1≡1(modp)a^{p-1}\equiv1\pmod p(Z/pZ)∗(\mathbb{Z}/p\mathbb{Z})^{*}, order p−1p-1
Any nnaφ(n)≡1(modn)a^{\varphi(n)}\equiv1\pmod n(Z/nZ)∗(\mathbb{Z}/n\mathbb{Z})^{*}, order φ(n)\varphi(n)

UndergraduateTheorems

If pp is prime and gcd⁡(a,p)=1\gcd(a,p)=1, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}. Equivalently, ap≡a(modp)a^{p} \equiv a \pmod{p} for every integer aa.

Why is it true?

The nonzero residues modulo a prime pp form a closed system under multiplication: multiplying every one of them by a fixed aa just shuffles them among themselves. Repeating this shuffle p−1p-1 times must therefore return every element to its own position, which is exactly the statement ap−1≡1(modp)a^{p-1}\equiv1\pmod p.

Proof

Consider the set S={1,2,…,p−1}S=\{1,2,\dots,p-1\} of nonzero residues modulo pp, and the map x↦ax mod px \mapsto ax \bmod p sending each element of SS to a residue.

This map is injective on SS: if ax≡ay(modp)ax\equiv ay\pmod p for x,y∈Sx,y\in S, then since gcd⁡(a,p)=1\gcd(a,p)=1, the cancellation law (proved in the previous topic) gives x≡y(modp)x\equiv y\pmod p, hence x=yx=y because both lie in {1,…,p−1}\{1,\dots,p-1\}. Also no axax can be ≡0(modp)\equiv 0\pmod p for x∈Sx\in S, since pp is prime and divides neither aa nor xx. So the map actually lands back inside SS.

An injective map from the finite set SS to itself is automatically a bijection. Therefore {a⋅1,a⋅2,…,a⋅(p−1)}(modp)\{a\cdot1,a\cdot2,\dots,a\cdot(p-1)\} \pmod p is simply {1,2,…,p−1}\{1,2,\dots,p-1\} written in a different order.

Multiply all p−1p-1 elements of both sets together. On one side we get (a⋅1)(a⋅2)⋯(a⋅(p−1))=ap−1 (p−1)!(a\cdot1)(a\cdot2)\cdots(a\cdot(p-1)) = a^{p-1}\,(p-1)!; on the other, since it's the same numbers reordered, we get exactly (p−1)!(p-1)!. So ap−1(p−1)!≡(p−1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod p.

Since pp is prime, none of 1,2,…,p−11,2,\dots,p-1 is divisible by pp, so gcd⁡((p−1)!,p)=1\gcd((p-1)!,p)=1. Applying the cancellation law once more to remove the common factor (p−1)!(p-1)! from both sides gives ap−1≡1(modp)a^{p-1}\equiv1\pmod p. ■\blacksquare

(Group-theoretic restatement: the nonzero residues form a group of order p−1p-1 under multiplication since pp is prime, and Lagrange's theorem says the order of any element — here, the smallest kk with ak≡1a^k\equiv1 — must divide the group's order p−1p-1; hence ap−1≡1(modp)a^{p-1}\equiv1\pmod p automatically.)

If gcd⁡(a,n)=1\gcd(a,n)=1, then aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}, where φ\varphi is Euler's totient function.

Why is it true?

This is exactly Fermat's argument with a general modulus nn: instead of all nonzero residues (which only form a closed multiplicative system when nn is prime), we restrict to the residues actually coprime to nn — the ones that have inverses — and the same shuffling argument applies to that smaller, closed set of size φ(n)\varphi(n).

Proof

Let R={r1,…,rφ(n)}R=\{r_1,\dots,r_{\varphi(n)}\} be a reduced residue system modulo nn: representatives of every residue class coprime to nn.

Multiplication by aa permutes RR: for each rir_i, gcd⁡(ari,n)=1\gcd(ar_i,n)=1 because gcd⁡(a,n)=1\gcd(a,n)=1 and gcd⁡(ri,n)=1\gcd(r_i,n)=1 (a product is coprime to nn iff both factors are), so ari mod nar_i \bmod n is again a member of the coprime residue classes; and the map ri↦ari mod nr_i\mapsto ar_i \bmod n is injective by the cancellation law, since gcd⁡(a,n)=1\gcd(a,n)=1.

An injective map from the finite set RR to itself (up to residue) is a bijection, so {ar1,…,arφ(n)}(modn)\{ar_1,\dots,ar_{\varphi(n)}\} \pmod n is just {r1,…,rφ(n)}\{r_1,\dots,r_{\varphi(n)}\} reordered.

Multiplying all φ(n)\varphi(n) elements of each set: aφ(n) (r1r2⋯rφ(n))≡r1r2⋯rφ(n)(modn)a^{\varphi(n)}\, (r_1 r_2\cdots r_{\varphi(n)}) \equiv r_1 r_2 \cdots r_{\varphi(n)} \pmod n. Since each rir_i is coprime to nn, so is their product R=∏riR=\prod r_i, i.e. gcd⁡(R,n)=1\gcd(R,n)=1.

Cancel RR from both sides (valid since gcd⁡(R,n)=1\gcd(R,n)=1) to get aφ(n)≡1(modn)a^{\varphi(n)}\equiv1\pmod n. ■\blacksquare

This is again Lagrange's theorem in disguise: the residues coprime to nn form a group of order φ(n)\varphi(n) under multiplication (the group of units (Z/nZ)∗(\mathbb{Z}/n\mathbb{Z})^{*}), and the order of any element divides φ(n)\varphi(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)\varphi(n) and getting back to 11. Fermat's little theorem gives a fast probabilistic primality test (try a base aa and check ap−1≡1a^{p-1}\equiv1), and lets one compute a modular inverse as ap−2 mod pa^{p-2}\bmod p 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=11p=5,q=11, so n=pq=55n=pq=55 and φ(n)=(p−1)(q−1)=40\varphi(n)=(p-1)(q-1)=40. Choose public exponent e=3e=3 (since gcd⁡(3,40)=1\gcd(3,40)=1) and private exponent d=27d=27 (since 3×27=81=2×40+13\times27=81=2\times40+1, so ed≡1(mod40)ed\equiv1\pmod{40}). Encrypt m=2m=2 as c=me mod nc=m^e\bmod n, then decrypt cc and check you recover m=2m=2.

Solution

Encryption: c=23 mod 55=8c = 2^3 \bmod 55 = 8.

Decryption raises the ciphertext to the private exponent: m′=cd mod n=827 mod 55m' = c^d \bmod n = 8^{27}\bmod 55. Because ed=1+kφ(n)ed = 1+k\varphi(n) for some integer kk (here 81=1+2×4081=1+2\times40), we have m′≡med=m1+kφ(n)=m⋅(mφ(n))k(modn)m' \equiv m^{ed} = m^{1+k\varphi(n)} = m\cdot(m^{\varphi(n)})^k \pmod n.

Since gcd⁡(m,n)=gcd⁡(2,55)=1\gcd(m,n)=\gcd(2,55)=1, Euler's theorem gives mφ(n)≡1(modn)m^{\varphi(n)}\equiv1\pmod n, so (mφ(n))k≡1k=1(modn)(m^{\varphi(n)})^k\equiv1^k=1\pmod n, and therefore m′≡m⋅1=m(modn)m'\equiv m\cdot1 = m \pmod n.

Numerically: 827 mod 558^{27}\bmod55 can be computed by repeated squaring (82=64≡98^2=64\equiv9, 84≡81≡268^4\equiv81\equiv26, 88≡262=676≡676−660=168^8\equiv26^2=676\equiv676-660=16, 816≡162=256≡256−220=368^{16}\equiv16^2=256\equiv256-220=36; then 827=816⋅88⋅82⋅81≡36×16×9×88^{27}=8^{16}\cdot8^8\cdot8^2\cdot8^1\equiv36\times16\times9\times8). Reducing step by step modulo 5555 gives exactly 22, confirming that decryption recovers the original message m=2m=2 — precisely as Euler's theorem guarantees for any message and any two primes p,qp,q, not just this example.

Example: A Fermat pseudoprime: when the test lies

Show that 341=11×31341=11\times31 (which is composite) still satisfies 2340≡1(mod341)2^{340}\equiv1\pmod{341} — the exact conclusion Fermat's little theorem would predict if 341341 were prime.

Solution

Work modulo each prime factor separately. Modulo 1111: since 1111 is prime and gcd⁡(2,11)=1\gcd(2,11)=1, Fermat gives 210≡1(mod11)2^{10}\equiv1\pmod{11}; since 340=10×34340=10\times34, we get 2340=(210)34≡134=1(mod11)2^{340}=(2^{10})^{34}\equiv1^{34}=1\pmod{11}.

Modulo 3131: here 25=32≡1(mod31)2^5=32\equiv1\pmod{31}, so 22 has order 55 modulo 3131 (a divisor of φ(31)=30\varphi(31)=30, consistent with Fermat/Euler). Since 340=5×68340=5\times68 is a multiple of 55, 2340=(25)68≡168=1(mod31)2^{340}=(2^5)^{68}\equiv1^{68}=1\pmod{31}.

So 2340≡12^{340}\equiv1 both modulo 1111 and modulo 3131. By the Chinese remainder theorem (the next topic), being ≡1\equiv1 modulo two coprime numbers 1111 and 3131 forces 2340≡1(mod341)2^{340}\equiv1\pmod{341} as well, even though 341341 is not prime.

A composite number nn that passes an−1≡1(modn)a^{n-1}\equiv1\pmod n for some base aa coprime to it is called a **Fermat pseudoprime to base aa**; 341341 is the smallest pseudoprime to base 22. 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=13p=13 and gcd⁡(a,13)=1\gcd(a,13)=1, what is a12(mod13)a^{12}\pmod{13}?

Compute φ(20)\varphi(20).

In RSA with n=55n=55, φ(n)=40\varphi(n)=40, and public exponent e=3e=3, the private exponent dd must satisfy:

341=11×31341=11\times31 satisfies 2340≡1(mod341)2^{340}\equiv1\pmod{341} despite being composite. What is this phenomenon called?

References

  1. Ronald L. Rivest, Adi Shamir, Leonard M. Adleman (1978). A Method for Obtaining Digital Signatures and Public-Key Cryptosystems · DOI:10.1145/359340.359342
  2. W. R. Alford, Andrew Granville, Carl Pomerance (1994). There are Infinitely Many Carmichael Numbers · DOI:10.2307/2118576