Skip to content

Fermat, Euler and Wilson Theorems

Theorem (Fermat). If pp is prime and p∤ap \nmid a, then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod p.

Equivalently, for every integer aa (including multiples of pp): ap≡a(modp)a^p \equiv a \pmod p.

Proof. The products a,2a,…,(p−1)aa, 2a, \ldots, (p-1)a are all distinct modulo pp (if ia≡jaia \equiv ja then p∣(i−j)ap \mid (i-j)a, and p∤ap \nmid a, so p∣i−jp \mid i - j with ∣i−j∣<p|i - j| < p). They are therefore a permutation of 1,2,…,p−11, 2, \ldots, p-1 modulo pp. Multiplying the p−1p-1 congruences ia≡kiia \equiv k_i together gives

ap−1(p−1)!≡(p−1)!(modp),a^{p-1}(p-1)! \equiv (p-1)! \pmod p,

and since p∤(p−1)!p \nmid (p-1)! we may cancel the factorial. ■\blacksquare

Uses. Exponent reduction modulo a prime (a100(mod7)a^{100} \pmod 7: reduce the exponent mod 6), and the primality test an−1≢1(modn)a^{n-1} \not\equiv 1 \pmod n certifies nn composite (the converse fails: Carmichael numbers satisfy Fermat for every base aa coprime to nn; 561 is the smallest).

4.2 Euler’s Totient Function and Euler’s Theorem

Section titled “4.2 Euler’s Totient Function and Euler’s Theorem”

Definition. φ(n)\varphi(n) counts the integers in {1,…,n}\{1, \ldots, n\} that are coprime to nn.

Key values and formulas:

  • φ(p)=p−1\varphi(p) = p - 1 for prime pp; φ(pk)=pk−pk−1=pk(1−1/p)\varphi(p^k) = p^k - p^{k-1} = p^k(1 - 1/p).
  • φ\varphi is multiplicative: gcd⁡(m,n)=1⇒φ(mn)=φ(m)φ(n)\gcd(m,n) = 1 \Rightarrow \varphi(mn) = \varphi(m)\varphi(n).
  • If n=p1e1⋯pkekn = p_1^{e_1} \cdots p_k^{e_k}, then φ(n)=n∏i(1−1pi)\varphi(n) = n \prod_{i} \left(1 - \frac{1}{p_i}\right).

Theorem (Euler). If gcd⁡(a,n)=1\gcd(a, n) = 1 then

aφ(n)≡1(modn).a^{\varphi(n)} \equiv 1 \pmod n.

Proof. The units of Z/nZ\mathbb{Z}/n\mathbb{Z} form a group of order φ(n)\varphi(n); multiplying all units by aa permutes the group (cancellation by the unit aa), and multiplying the permuted products gives aφ(n)⋅(product of units)≡(product of units)a^{\varphi(n)} \cdot (\text{product of units}) \equiv (\text{product of units}); cancel. ■\blacksquare

Fermat is the special case n=pn = p, φ(p)=p−1\varphi(p) = p-1. Euler also handles composite moduli: 3100(mod10)3^{100} \pmod{10} has φ(10)=4\varphi(10) = 4, 100≡0(mod4)100 \equiv 0 \pmod 4… careful; reduce 100=4⋅25100 = 4 \cdot 25, so 3100=(34)25≡125=1(mod10)3^{100} = (3^4)^{25} \equiv 1^{25} = 1 \pmod{10}.

Negative and large exponents. When gcd⁡(a,n)=1\gcd(a, n) = 1 the inverse of aea^e is aφ(n)−ea^{\varphi(n)-e}: compute 7−1(mod20)7^{-1} \pmod{20} as 7φ(20)−1=777^{\varphi(20)-1} = 7^{7}, or directly via the extended Euclidean algorithm (7⋅3=21≡17 \cdot 3 = 21 \equiv 1, so 7−1≡37^{-1} \equiv 3).

Theorem (Wilson). For p>1p > 1:

(p−1)!≡−1(modp)  ⟺  p is prime.(p-1)! \equiv -1 \pmod p \iff p \text{ is prime.}

Proof (⇒\Rightarrow). Pair each k∈{1,…,p−1}k \in \{1, \ldots, p-1\} with its inverse modulo pp. Only 1 and p−1p-1 are their own inverses (k2≡1k^2 \equiv 1 means p∣(k−1)(k+1)p \mid (k-1)(k+1), so k≡±1k \equiv \pm 1). The remaining p−3p - 3 elements pair off into inverse pairs with product 1, leaving (p−1)!≡1⋅(p−1)≡−1(p-1)! \equiv 1 \cdot (p-1) \equiv -1. ■\blacksquare

(⇐\Leftarrow is a nice exercise: if (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n then every proper divisor of nn cancels in (n−1)!(n-1)!, so nn has no proper divisor >1> 1.)

Applications.

  • (p−2)!≡1(modp)(p-2)! \equiv 1 \pmod p: multiply Wilson by p−1≡−1p - 1 \equiv -1.
  • Wilson gives the classical formula for the number of ways to order a complete residue system, and certifies primality; though it is useless as a practical test (factorials are enormous).

Problem 1. Find the remainder of 25002^{500} on division by 13.

φ(13)=12\varphi(13) = 12 and 500=12⋅41+8500 = 12 \cdot 41 + 8, so 2500≡28=256≡9(mod13)2^{500} \equiv 2^8 = 256 \equiv 9 \pmod{13}.

Problem 2. Show 30∣n5−n30 \mid n^5 - n for every integer nn.

n5−n=n(n4−1)n^5 - n = n(n^4 - 1). Fermat for 2, 3 and 5: n5≡n(modp)n^5 \equiv n \pmod p for p=2,3,5p = 2, 3, 5 (directly or by n5−n=n(n4−1)n^5 - n = n(n^4-1) and p−1∣4p - 1 \mid 4 for p∈{2,3,5}p \in \{2,3,5\}… more precisely p−1∈{1,2,4}p - 1 \in \{1, 2, 4\} all divide 4). Since 2, 3, 5 are pairwise coprime, their product 30 divides n5−nn^5 - n.

Problem 3. Solve 4340(mod341)4^{340} \pmod{341} where 341=11⋅31341 = 11 \cdot 31.

Fermat for 11: 410≡14^{10} \equiv 1, and 340=34⋅10340 = 34 \cdot 10, so 4340≡1(mod11)4^{340} \equiv 1 \pmod{11}. Fermat for 31: 430≡14^{30} \equiv 1, and 340≡10(mod30)340 \equiv 10 \pmod{30}, so 4340≡410=10485764^{340} \equiv 4^{10} = 1048576. Then 1048576=33824⋅31+121048576 = 33824 \cdot 31 + 12, so 4340≡12(mod31)4^{340} \equiv 12 \pmod{31}. Combine by CRT (see the next page): the answer is congruent to 1 mod 11 and 12 mod 31. (341 is a Poulet number: 4340≡1(mod341)4^{340} \equiv 1 \pmod{341} despite being composite.)

  • Fermat requires prime modulus and (in the ap−1a^{p-1} form) gcd⁡(a,p)=1\gcd(a, p) = 1.
  • Euler requires coprimality: 2100(mod12)2^{100} \pmod{12} cannot be reduced with φ(12)=4\varphi(12) = 4 because gcd⁡(2,12)≠1\gcd(2, 12) \neq 1. Instead track the residue directly (2100≡0(mod4)2^{100} \equiv 0 \pmod 4 and 2100≡4(mod3)2^{100} \equiv 4 \pmod 3, giving 8≡8(mod12)8 \equiv 8 \pmod{12} by CRT).
  • φ\varphi is not multiplicative when gcd⁡(m,n)≠1\gcd(m, n) \neq 1: φ(8)=4≠φ(4)φ(2)=4\varphi(8) = 4 \neq \varphi(4)\varphi(2) = 4 is fine, but φ(4)φ(2)=2⋅1=2≠φ(8)=4\varphi(4)\varphi(2) = 2 \cdot 1 = 2 \neq \varphi(8) = 4 shows the multiplication rule breaks without coprimality.
  • Wilson is an iff: the failure direction is a legitimate one-line primality certificate.