Theorem (Fermat). If p is prime and p∤a, then
ap−1≡1(modp).Equivalently, for every integer a (including multiples of p): ap≡a(modp).
Proof. The products a,2a,…,(p−1)a are all distinct modulo p (if ia≡ja then p∣(i−j)a, and p∤a, so p∣i−j with ∣i−j∣<p). They are therefore a permutation of 1,2,…,p−1 modulo p. Multiplying the p−1 congruences ia≡ki together gives
ap−1(p−1)!≡(p−1)!(modp),and since p∤(p−1)! we may cancel the factorial. ■
Uses. Exponent reduction modulo a prime (a100(mod7): reduce the exponent mod 6), and the primality test an−1≡1(modn) certifies n composite (the converse fails: Carmichael numbers satisfy Fermat for every base a coprime to n; 561 is the smallest).
Definition. φ(n) counts the integers in {1,…,n} that are coprime to n.
Key values and formulas:
- φ(p)=p−1 for prime p; φ(pk)=pk−pk−1=pk(1−1/p).
- φ is multiplicative: gcd(m,n)=1⇒φ(mn)=φ(m)φ(n).
- If n=p1e1⋯pkek, then φ(n)=n∏i(1−pi1).
Theorem (Euler). If gcd(a,n)=1 then
aφ(n)≡1(modn).Proof. The units of Z/nZ form a group of order φ(n); multiplying all units by a permutes the group (cancellation by the unit a), and multiplying the permuted products gives aφ(n)⋅(product of units)≡(product of units); cancel. ■
Fermat is the special case n=p, φ(p)=p−1. Euler also handles composite moduli: 3100(mod10) has φ(10)=4, 100≡0(mod4)… careful; reduce 100=4⋅25, so 3100=(34)25≡125=1(mod10).
Negative and large exponents. When gcd(a,n)=1 the inverse of ae is aφ(n)−e: compute 7−1(mod20) as 7φ(20)−1=77, or directly via the extended Euclidean algorithm (7⋅3=21≡1, so 7−1≡3).
Theorem (Wilson). For p>1:
(p−1)!≡−1(modp)⟺p is prime.Proof (⇒). Pair each k∈{1,…,p−1} with its inverse modulo p. Only 1 and p−1 are their own inverses (k2≡1 means p∣(k−1)(k+1), so k≡±1). The remaining p−3 elements pair off into inverse pairs with product 1, leaving (p−1)!≡1⋅(p−1)≡−1. ■
(⇐ is a nice exercise: if (n−1)!≡−1(modn) then every proper divisor of n cancels in (n−1)!, so n has no proper divisor >1.)
Applications.
- (p−2)!≡1(modp): multiply Wilson by p−1≡−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 2500 on division by 13.
φ(13)=12 and 500=12⋅41+8, so 2500≡28=256≡9(mod13).
Problem 2. Show 30∣n5−n for every integer n.
n5−n=n(n4−1). Fermat for 2, 3 and 5: n5≡n(modp) for p=2,3,5 (directly or by n5−n=n(n4−1) and p−1∣4 for p∈{2,3,5}… more precisely p−1∈{1,2,4} all divide 4). Since 2, 3, 5 are pairwise coprime, their product 30 divides n5−n.
Problem 3. Solve 4340(mod341) where 341=11⋅31.
Fermat for 11: 410≡1, and 340=34⋅10, so 4340≡1(mod11). Fermat for 31: 430≡1, and 340≡10(mod30), so 4340≡410=1048576. Then 1048576=33824⋅31+12, so 4340≡12(mod31). 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) despite being composite.)
- Fermat requires prime modulus and (in the ap−1 form) gcd(a,p)=1.
- Euler requires coprimality: 2100(mod12) cannot be reduced with φ(12)=4 because gcd(2,12)=1. Instead track the residue directly (2100≡0(mod4) and 2100≡4(mod3), giving 8≡8(mod12) by CRT).
- φ is not multiplicative when gcd(m,n)=1: φ(8)=4=φ(4)φ(2)=4 is fine, but φ(4)φ(2)=2⋅1=2=φ(8)=4 shows the multiplication rule breaks without coprimality.
- Wilson is an iff: the failure direction is a legitimate one-line primality certificate.