Fermat's Little Theorem

Contents

This note continues the Congruence note. As before, all variables are integers unless otherwise specified, and the modulus mm is always a positive integer.

Complete and Reduced Residue Systems

Definition 1 (Complete Residue System).

Let m∈Nm \in \bbN. A complete residue system modulo mm is a set of mm integers

S={a1,a2,…,am}S = \left\{ a_1, a_2, \ldots, a_m \right\}

containing exactly one representative from each residue class modulo mm. In other words, every a∈Za \in \bbZ is congruent modulo mm to exactly one element aia_i of SS.

The typical example of a complete residue system modulo mm is the set of remainders

{0,1,2,…,m−1}.\left\{ 0, 1, 2, \ldots, m - 1 \right\}.

Another one modulo 55 is {10,−4,7,18,4}\{ 10, -4, 7, 18, 4 \}, since these numbers leave the remainders 0,1,2,3,40, 1, 2, 3, 4 (in that order) upon division by 55.

Definition 2 (Reduced Residue System).

Let m∈Nm \in \bbN. A reduced residue system modulo mm is a set of integers

T={a1,a2,…,ak}T = \left\{ a_1, a_2, \ldots, a_k \right\}

that are pairwise incongruent modulo mm, each coprime to mm, and such that every integer coprime to mm is congruent to one of them. In other words, every a∈Za \in \bbZ with gcd⁡(a,m)=1\gcd(a, m) = 1 is congruent modulo mm to exactly one element aia_i of TT.

The typical example of a reduced residue system modulo mm is obtained from {0,1,…,m−1}\{ 0, 1, \ldots, m - 1 \} by keeping only the numbers coprime to mm:

{a∈Z ⁣:0≤a≤m−1 and gcd⁡(a,m)=1}.\left\{ a \in \bbZ \colon 0 \leq a \leq m - 1 \text{ and } \gcd(a, m) = 1 \right\}.

For instance, {1,3,7,9}\{ 1, 3, 7, 9 \} is a reduced residue system modulo 1010, and {1,2,…,p−1}\{ 1, 2, \ldots, p - 1 \} is a reduced residue system modulo any prime pp. Since a reduced residue system contains exactly one representative of each residue class coprime to mm, any two reduced residue systems modulo mm have the same number of elements.

Proposition 3.

If gcd⁡(a,m)=1\gcd(a, m) = 1 and gcd⁡(b,m)=1\gcd(b, m) = 1, then gcd⁡(ab,m)=1\gcd(ab, m) = 1. Consequently, a reduced residue system modulo mm is closed under multiplication modulo mm: the product of any two of its elements is congruent modulo mm to an element of the system.

Proof.

By the Existence of Multiplicative Inverse (Proposition 12 of the Congruence note), a−1a^{-1} and b−1b^{-1} exist modulo mm. Then (ab)(b−1a−1)≡a(bb−1)a−1≡aa−1≡1(modm)(ab)(b^{-1} a^{-1}) \equiv a (b b^{-1}) a^{-1} \equiv a a^{-1} \equiv 1 \pmod{m}, so abab has a multiplicative inverse modulo mm, and therefore gcd⁡(ab,m)=1\gcd(ab, m) = 1. The second statement follows from the definition of a reduced residue system.

Example 4.

When m=10m = 10, a reduced residue system is {1,3,7,9}\{ 1, 3, 7, 9 \}. Its multiplication table modulo 1010 is

×\times1379
11379
33917
77193
99731

For example, 3×7=21≡1(mod10)3 \times 7 = 21 \equiv 1 \pmod{10} and 7×9=63≡3(mod10)7 \times 9 = 63 \equiv 3 \pmod{10}. Every entry is again 11, 33, 77, or 99. Notice also that each row is a rearrangement of 1,3,7,91, 3, 7, 9. This observation, stated for a general modulus in the lemma below, is the key to the proofs of Fermat’s and Euler’s theorems.

Lemma 5.

Let T={r1,r2,…,rk}T = \{ r_1, r_2, \ldots, r_k \} be a reduced residue system modulo mm, and let gcd⁡(a,m)=1\gcd(a, m) = 1. Then

aT={ar1,ar2,…,ark}aT = \left\{ a r_1, a r_2, \ldots, a r_k \right\}

is also a reduced residue system modulo mm. Consequently,

r1r2⋯rk≡(ar1)(ar2)⋯(ark)=ak r1r2⋯rk(modm).r_1 r_2 \cdots r_k \equiv (a r_1)(a r_2) \cdots (a r_k) = a^k \, r_1 r_2 \cdots r_k \pmod{m}.
Proof.

By Proposition 3, each aria r_i is coprime to mm. If ari≡arj(modm)a r_i \equiv a r_j \pmod{m}, then the Cancellation Law (Proposition 10 of the Congruence note) gives ri≡rj(modm)r_i \equiv r_j \pmod{m}; since the elements of TT are pairwise incongruent, i=ji = j. Thus aTaT consists of kk integers coprime to mm lying in kk different residue classes. But there are exactly kk residue classes coprime to mm, one for each element of TT, so aTaT meets every one of them and is therefore a reduced residue system.

Hence each aria r_i is congruent to exactly one rjr_j, and different ii give different jj. In other words, ar1,ar2,…,arka r_1, a r_2, \ldots, a r_k are congruent to r1,r2,…,rkr_1, r_2, \ldots, r_k in some rearranged order, so their products are congruent modulo mm.

Multiplicative Order

Definition 6 (Multiplicative Order).

Let m∈Nm \in \bbN and a∈Za \in \bbZ with gcd⁡(a,m)=1\gcd(a, m) = 1. The multiplicative order of aa modulo mm, written ord⁡m(a)\ord_m(a), is the smallest positive integer kk such that ak≡1(modm)a^k \equiv 1 \pmod{m}.

Such a kk always exists. Indeed, by Proposition 3, the powers a,a2,a3,…a, a^2, a^3, \ldots are all coprime to mm, and there are only finitely many residue classes modulo mm, so two of them must coincide: ai≡aj(modm)a^i \equiv a^j \pmod{m} for some i<ji < j. Since gcd⁡(ai,m)=1\gcd(a^i, m) = 1, the Cancellation Law (Proposition 10 of the Congruence note) gives aj−i≡1(modm)a^{j - i} \equiv 1 \pmod{m} with j−i>0j - i > 0.

Example 7.

We have ord⁡10(3)=4\ord_{10}(3) = 4, because

31≡3,32≡9,33≡7,34≡1(mod10).3^1 \equiv 3, \quad 3^2 \equiv 9, \quad 3^3 \equiv 7, \quad 3^4 \equiv 1 \pmod{10}.

Similarly, we can compute the order of every element of the reduced residue system {1,3,7,9}\{ 1, 3, 7, 9 \}:

ord⁡10(1)=1,ord⁡10(3)=4,ord⁡10(7)=4,ord⁡10(9)=2.\begin{aligned} \ord_{10}(1) &= 1, \\ \ord_{10}(3) &= 4, \\ \ord_{10}(7) &= 4, \\ \ord_{10}(9) &= 2. \end{aligned}

The powers of aa modulo mm repeat in a cycle whose length is exactly ord⁡m(a)\ord_m(a). For instance, the units digits of 31,32,33,…3^1, 3^2, 3^3, \ldots are 3,9,7,1,3,9,7,1,…3, 9, 7, 1, 3, 9, 7, 1, \ldots, with period 44. The following proposition makes this precise.

Proposition 8.

Let m∈Nm \in \bbN and a∈Za \in \bbZ with gcd⁡(a,m)=1\gcd(a, m) = 1, and let kk be a non-negative integer. Then

ak≡1(modm)  ⇔  ord⁡m(a)∣k.a^k \equiv 1 \pmod{m} \;\Leftrightarrow\; \ord_m(a) \divides k.
Proof.

Let d=ord⁡m(a)d = \ord_m(a). If d∣kd \divides k, say k=dqk = dq, then ak=(ad)q≡1q=1(modm)a^k = \left(a^d\right)^q \equiv 1^q = 1 \pmod{m}.

Conversely, suppose ak≡1(modm)a^k \equiv 1 \pmod{m}. By the division algorithm, write k=dq+rk = dq + r with 0≤r<d0 \leq r < d. Then

1≡ak=(ad)qar≡ar(modm).1 \equiv a^k = \left(a^d\right)^q a^r \equiv a^r \pmod{m}.

Since dd is the smallest positive integer with ad≡1(modm)a^d \equiv 1 \pmod{m} and 0≤r<d0 \leq r < d, we must have r=0r = 0. Hence d∣kd \divides k.

A useful consequence: if ak≡1(modm)a^k \equiv 1 \pmod{m}, then an≡an mod k(modm)a^n \equiv a^{n \bmod k} \pmod{m} for every non-negative integer nn. So to compute a large power modulo mm, it suffices to reduce the exponent modulo any kk with ak≡1(modm)a^k \equiv 1 \pmod{m}.

Remark (Computing the order). Proposition 8 also gives a quick way to pin down an order. Suppose ad≡1(modm)a^d \equiv 1 \pmod{m}, so that ord⁡m(a)∣d\ord_m(a) \divides d. If ord⁡m(a)\ord_m(a) were a proper divisor of dd, then it would divide d/qd/q for some prime q∣dq \divides d, and so ad/q≡1(modm)a^{d/q} \equiv 1 \pmod{m}. Therefore, if ad≡1(modm)a^d \equiv 1 \pmod{m} and ad/q≢1(modm)a^{d/q} \not\equiv 1 \pmod{m} for every prime q∣dq \divides d, then ord⁡m(a)=d\ord_m(a) = d. For example, 34=81≡1(mod10)3^4 = 81 \equiv 1 \pmod{10}, and the only prime factor of 44 is 22, with 34/2=9≢1(mod10)3^{4/2} = 9 \not\equiv 1 \pmod{10}. Hence ord⁡10(3)=4\ord_{10}(3) = 4, confirming Example 7 without checking 313^1 and 333^3.

Fermat’s Little Theorem

Theorem 9 (Fermat's Little Theorem).

Let pp be a prime and a∈Za \in \bbZ with p∤ap \nmid a. Then

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

In other words, by Proposition 8, ord⁡p(a)∣(p−1)\ord_p(a) \divides (p - 1).

Proof.

Since pp is prime, T={1,2,…,p−1}T = \{ 1, 2, \ldots, p - 1 \} is a reduced residue system modulo pp, and since p∤ap \nmid a, we have gcd⁡(a,p)=1\gcd(a, p) = 1. Lemma 5 with k=p−1k = p - 1 gives

(p−1)!=1×2×⋯×(p−1)≡a×(2a)×⋯×(p−1)a=ap−1(p−1)!(modp).(p - 1)! = 1 \times 2 \times \cdots \times (p - 1) \equiv a \times (2a) \times \cdots \times (p - 1)a = a^{p-1} (p - 1)! \pmod{p}.

Since gcd⁡((p−1)!,p)=1\gcd\left((p - 1)!, p\right) = 1 by Proposition 3, the Cancellation Law gives ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}.

Corollary 10.

For any prime pp and any a∈Za \in \bbZ, ap≡a(modp)a^p \equiv a \pmod{p}.

Proof.

If p∤ap \nmid a, multiply both sides of ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p} by aa. If p∣ap \divides a, then both apa^p and aa are congruent to 00 modulo pp.

Remark. The hypothesis that pp is prime is essential, even when aa is coprime to the modulus. For example, 34−1=27≡3(mod4)3^{4 - 1} = 27 \equiv 3 \pmod{4} and 56−1=3125≡5(mod6)5^{6 - 1} = 3125 \equiv 5 \pmod{6}, not 11. As we will see in the section on Euler’s Theorem, the right exponent for a general modulus mm is not m−1m - 1 but φ(m)\varphi(m), the number of residue classes coprime to mm. Indeed, 32≡1(mod4)3^2 \equiv 1 \pmod{4} and 52≡1(mod6)5^2 \equiv 1 \pmod{6}, and in both cases there are exactly 22 such classes.

Remark (Inverses). Fermat’s Little Theorem also gives a formula for multiplicative inverses modulo a prime: since a×ap−2=ap−1≡1(modp)a \times a^{p-2} = a^{p-1} \equiv 1 \pmod{p}, we have a−1≡ap−2(modp)a^{-1} \equiv a^{p-2} \pmod{p} whenever p∤ap \nmid a. For example, 3−1≡35=243=34×7+5≡5(mod7)3^{-1} \equiv 3^5 = 243 = 34 \times 7 + 5 \equiv 5 \pmod{7}; check: 3×5=15≡1(mod7)3 \times 5 = 15 \equiv 1 \pmod{7}.

Example 11.

Find the remainder when 320263^{2026} is divided by 77.

Solution

Since 77 is prime and 7∤37 \nmid 3, Fermat’s Little Theorem gives 36≡1(mod7)3^6 \equiv 1 \pmod{7}. Since 2026=6×337+42026 = 6 \times 337 + 4,

32026=(36)337×34≡1337×81≡81=11×7+4≡4(mod7).3^{2026} = \left(3^6\right)^{337} \times 3^4 \equiv 1^{337} \times 81 \equiv 81 = 11 \times 7 + 4 \equiv 4 \pmod{7}.

The remainder is 44.

Exercise 12.

Find the remainder when 220262^{2026} is divided by 1111.

Exercise 13.

Compute ord⁡7(a)\ord_7(a) for a=1,2,…,6a = 1, 2, \ldots, 6, and verify that each one divides 66.

Exercise 14.

Prove that 30∣(n5−n)30 \divides (n^5 - n) for every integer nn.

Hint: Since 30=2×3×530 = 2 \times 3 \times 5 and these primes are pairwise coprime, it suffices to show p∣(n5−n)p \divides (n^5 - n) for p=2,3,5p = 2, 3, 5. Use Corollary 10.

Answers

12. By Fermat’s Little Theorem, 210≡1(mod11)2^{10} \equiv 1 \pmod{11}. Since 2026=10×202+62026 = 10 \times 202 + 6, we get 22026≡26=64=5×11+9≡9(mod11)2^{2026} \equiv 2^6 = 64 = 5 \times 11 + 9 \equiv 9 \pmod{11}. The remainder is 99.

13. ord⁡7(1)=1\ord_7(1) = 1; ord⁡7(2)=3\ord_7(2) = 3 since 23=8≡12^3 = 8 \equiv 1; ord⁡7(3)=6\ord_7(3) = 6 since 3,2,6,4,5,13, 2, 6, 4, 5, 1 are the successive powers; ord⁡7(4)=3\ord_7(4) = 3 since 43=64≡14^3 = 64 \equiv 1; ord⁡7(5)=6\ord_7(5) = 6 since 5,4,6,2,3,15, 4, 6, 2, 3, 1 are the successive powers; ord⁡7(6)=2\ord_7(6) = 2 since 62=36≡16^2 = 36 \equiv 1. The orders 1,3,6,3,6,21, 3, 6, 3, 6, 2 all divide 66.

14. For p=5p = 5, Corollary 10 gives n5≡n(mod5)n^5 \equiv n \pmod{5} directly. For p=3p = 3, n3≡n(mod3)n^3 \equiv n \pmod{3}, so n5=n3×n2≡n×n2=n3≡n(mod3)n^5 = n^3 \times n^2 \equiv n \times n^2 = n^3 \equiv n \pmod{3}. For p=2p = 2, n2≡n(mod2)n^2 \equiv n \pmod{2}, so n5≡n(mod2)n^5 \equiv n \pmod{2} by applying this repeatedly. Since 22, 33, 55 all divide n5−nn^5 - n and are pairwise coprime, 30∣(n5−n)30 \divides (n^5 - n).

Practice Problems

  1. What is the remainder when 520265^{2026} is divided by 1313?

    (A) 1(B) 5(C) 8(D) 11(E) 12\textup{(A)}\ 1 \qquad \textup{(B)}\ 5 \qquad \textup{(C)}\ 8 \qquad \textup{(D)}\ 11 \qquad \textup{(E)}\ 12
  2. What is the remainder when 112+212+312+⋯+100121^{12} + 2^{12} + 3^{12} + \cdots + 100^{12} is divided by 1313?

    (A) 0(B) 1(C) 2(D) 7(E) 12\textup{(A)}\ 0 \qquad \textup{(B)}\ 1 \qquad \textup{(C)}\ 2 \qquad \textup{(D)}\ 7 \qquad \textup{(E)}\ 12
  3. What is the largest integer that divides n13−nn^{13} - n for every integer nn?

    (A) 26(B) 182(C) 390(D) 2730(E) 8190\textup{(A)}\ 26 \qquad \textup{(B)}\ 182 \qquad \textup{(C)}\ 390 \qquad \textup{(D)}\ 2730 \qquad \textup{(E)}\ 8190

Solutions

Problem 1

(E) 12. By Fermat’s Little Theorem, 512≡1(mod13)5^{12} \equiv 1 \pmod{13}. Since 2026=12×168+102026 = 12 \times 168 + 10, we get 52026≡510(mod13)5^{2026} \equiv 5^{10} \pmod{13}. Now 52=25≡−1(mod13)5^2 = 25 \equiv -1 \pmod{13}, so 510=(52)5≡(−1)5=−1≡12(mod13)5^{10} = \left(5^2\right)^5 \equiv (-1)^5 = -1 \equiv 12 \pmod{13}.

Problem 2

(C) 2. By Fermat’s Little Theorem, k12≡1(mod13)k^{12} \equiv 1 \pmod{13} whenever 13∤k13 \nmid k, while k12≡0(mod13)k^{12} \equiv 0 \pmod{13} whenever 13∣k13 \divides k. Among 1,2,…,1001, 2, \ldots, 100, the multiples of 1313 are 13,26,…,9113, 26, \ldots, 91, which are 77 numbers. So the sum is congruent to 100−7=93=7×13+2≡2(mod13)100 - 7 = 93 = 7 \times 13 + 2 \equiv 2 \pmod{13}.

Problem 3

(D) 2730. Let pp be a prime with (p−1)∣12(p - 1) \divides 12, namely p=2,3,5,7,13p = 2, 3, 5, 7, 13. If p∤np \nmid n, Fermat’s Little Theorem gives np−1≡1(modp)n^{p-1} \equiv 1 \pmod{p}, so n12=(np−1)12/(p−1)≡1(modp)n^{12} = \left(n^{p-1}\right)^{12/(p-1)} \equiv 1 \pmod{p} and n13≡n(modp)n^{13} \equiv n \pmod{p}. If p∣np \divides n, then n13≡0≡n(modp)n^{13} \equiv 0 \equiv n \pmod{p}. Hence each of these pairwise coprime primes divides n13−nn^{13} - n, and so does their product 2×3×5×7×13=27302 \times 3 \times 5 \times 7 \times 13 = 2730.

Conversely, let DD be any integer that divides n13−nn^{13} - n for every nn. Taking n=2n = 2, DD divides 213−2=8190=2×32×5×7×132^{13} - 2 = 8190 = 2 \times 3^2 \times 5 \times 7 \times 13. Taking n=3n = 3, DD divides 313−3=3(312−1)3^{13} - 3 = 3\left(3^{12} - 1\right), which is not divisible by 99, so 9∤D9 \nmid D. Hence D∣8190/3=2730D \divides 8190 / 3 = 2730, and the largest such integer is 27302730.