Math Core

Module 3.1 · Number Theory

Fermat's and Euler's theorems

Almost every AIME has a problem that asks for the remainder of an enormous power, like 720267^{2026} or a tower such as 7777^{7^7}, when divided by some modulus. You can't compute these numbers, but you don't have to. Fermat's and Euler's theorems tell you that powers repeat, and exactly how often, so a giant exponent shrinks to a small one.

Fermat's little theorem

Fermat's little theorem

If pp is prime and p∤ap \nmid a, 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 (including multiples of pp).

Why it's true. Look at the p−1p - 1 numbers

a, 2a, 3a, …, (p−1)a.a, \ 2a, \ 3a, \ \dots, \ (p-1)a.

None of them is divisible by pp, and no two are congruent mod pp: if ia≡jaia \equiv ja, then p∣(i−j)ap \mid (i - j)a, and since p∤ap \nmid a we get p∣i−jp \mid i - j, which forces i=ji = j. So these p−1p - 1 numbers are just 1,2,…,p−11, 2, \dots, p-1 in some scrambled order mod pp. Multiply them all together:

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

Since (p−1)!(p-1)! is not divisible by pp, you can cancel it, leaving ap−1≡1a^{p-1} \equiv 1.

How to use it. Mod a prime pp, exponents only matter mod p−1p - 1. To find 5100 mod 75^{100} \bmod 7: 56≡15^6 \equiv 1, and 100=6⋅16+4100 = 6 \cdot 16 + 4, so 5100≡54=625≡2(mod7)5^{100} \equiv 5^4 = 625 \equiv 2 \pmod 7.

Euler's theorem

For a modulus that isn't prime, you need to know how many residues are invertible.

Definition

Euler's totient function

φ(n)\varphi(n) is the number of integers in {1,2,…,n}\{1, 2, \dots, n\} that are relatively prime to nn. If n=p1e1p2e2⋯pkekn = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}, then

φ(n)=n(1−1p1)(1−1p2)⋯(1−1pk)=∏piei−1(pi−1).\varphi(n) = n\left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right)\cdots\left(1 - \frac{1}{p_k}\right) = \prod p_i^{e_i - 1}(p_i - 1).

For a prime power, φ(pe)=pe−pe−1\varphi(p^e) = p^e - p^{e-1}: out of pep^e numbers, the multiples of pp are the only ones that fail. For general nn, φ\varphi is multiplicative: φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n) when gcd⁡(m,n)=1\gcd(m, n) = 1. That follows from the Chinese remainder theorem (next module): a residue mod mnmn is coprime to mnmn exactly when its residues mod mm and mod nn are coprime to mm and nn. Useful values: φ(100)=40\varphi(100) = 40, φ(125)=100\varphi(125) = 100, φ(1000)=400\varphi(1000) = 400.

Euler's theorem

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

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

The proof is the same as Fermat's. Let r1,…,rφ(n)r_1, \dots, r_{\varphi(n)} be the residues coprime to nn. Multiplying each by aa permutes them (same cancellation argument), so aφ(n)∏ri≡∏ria^{\varphi(n)} \prod r_i \equiv \prod r_i, and the product is invertible mod nn. Fermat is the special case n=pn = p, φ(p)=p−1\varphi(p) = p - 1.

Common mistake

Euler's theorem needs gcd⁡(a,n)=1\gcd(a, n) = 1. For 22026 mod 10002^{2026} \bmod 1000 you cannot reduce the exponent mod 400400, because gcd⁡(2,1000)≠1\gcd(2, 1000) \ne 1. Split 1000=8⋅1251000 = 8 \cdot 125 instead: 22026≡0(mod8)2^{2026} \equiv 0 \pmod 8, and Euler works mod 125125. Then combine with the Chinese remainder theorem.

Also, φ(n)\varphi(n) is a period, not necessarily the smallest period. Mod 10001000, 720≡17^{20} \equiv 1 already, even though φ(1000)=400\varphi(1000) = 400. Finding the smallest period is the subject of the module on orders.

Wilson's theorem

Wilson's theorem

If pp is prime, then (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p.

Why. Each of 1,2,…,p−11, 2, \dots, p-1 has an inverse mod pp. The only numbers that are their own inverse satisfy x2≡1x^2 \equiv 1, that is, p∣(x−1)(x+1)p \mid (x-1)(x+1), so x≡±1x \equiv \pm 1. All the other factors in (p−1)!(p-1)! pair off with their inverses, and each pair multiplies to 11. What's left is 1⋅(p−1)≡−11 \cdot (p-1) \equiv -1.

Wilson's theorem is how you handle factorials mod a prime: (p−2)!≡1(p-2)! \equiv 1, and in general you peel off the top few factors, which are ≡−1,−2,−3,…\equiv -1, -2, -3, \dots.

Worked examples

Worked example: Last two digits

Find the last two digits of 320263^{2026}.

Since gcd⁡(3,100)=1\gcd(3, 100) = 1 and φ(100)=40\varphi(100) = 40, you have 340≡1(mod100)3^{40} \equiv 1 \pmod{100}. Now 2026=40⋅50+262026 = 40 \cdot 50 + 26, so 32026≡3263^{2026} \equiv 3^{26}.

Compute: 310=59049≡493^{10} = 59049 \equiv 49, so 320≡492=2401≡1(mod100)3^{20} \equiv 49^2 = 2401 \equiv 1 \pmod{100}. (The true period is 2020, even better.) Then 326≡36=729≡293^{26} \equiv 3^6 = 729 \equiv 29.

The last two digits are 2929.

Worked example: A tower

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

Mod 1111, 210≡12^{10} \equiv 1, so you only need the exponent 320263^{2026} mod 1010. Powers of 33 mod 1010 cycle 3,9,7,13, 9, 7, 1 with period 44, and 2026≡2(mod4)2026 \equiv 2 \pmod 4, so 32026≡9(mod10)3^{2026} \equiv 9 \pmod{10}.

So 232026≡29=512=11⋅46+62^{3^{2026}} \equiv 2^9 = 512 = 11 \cdot 46 + 6. The remainder is 66.

The pattern for towers: to reduce abca^{b^c} mod nn, reduce the exponent bcb^c mod φ(n)\varphi(n) (or a smaller period), which is again a power problem with a smaller modulus.

Worked example: Wilson's theorem

Find the remainder when 99!99! is divided by 101101.

By Wilson, 100!≡−1(mod101)100! \equiv -1 \pmod{101}. But 100!=99!⋅100100! = 99! \cdot 100 and 100≡−1100 \equiv -1, so −99!≡−1-99! \equiv -1, giving 99!≡199! \equiv 1. The remainder is 11.

Worked example: A divisibility for all n

Prove that n7−nn^7 - n is divisible by 4242 for every integer nn.

42=2⋅3⋅742 = 2 \cdot 3 \cdot 7. By Fermat, n7≡n(mod7)n^7 \equiv n \pmod 7. Mod 33: n3≡nn^3 \equiv n, so n7=n3⋅n3⋅n≡n⋅n⋅n=n3≡nn^7 = n^3 \cdot n^3 \cdot n \equiv n \cdot n \cdot n = n^3 \equiv n. Mod 22: n7n^7 and nn have the same parity. So 22, 33 and 77 all divide n7−nn^7 - n, and so does their product.

In general, p∣nk−np \mid n^k - n for all nn whenever (p−1)∣(k−1)(p-1) \mid (k-1).

Tip

Mod 10001000, always split into mod 88 and mod 125125. Powers of 22 die mod 88 quickly, and mod 125125 you have φ(125)=100\varphi(125) = 100, so exponents reduce mod 100100.

Practice

Practice 1

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

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

Find the remainder when 112+212+312+⋯+2026121^{12} + 2^{12} + 3^{12} + \cdots + 2026^{12} is divided by 1313.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Find the remainder when 720267^{2026} is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

Find the remainder when 94!94! is divided by 9797.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

Find the largest positive integer dd such that dd divides n11−nn^{11} - n for every integer nn.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

Find the last three digits of 7777^{7^7}.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Find the sum of all positive integers nn such that φ(n)=24\varphi(n) = 24.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Find the remainder when 2220262^{2^{2026}} is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice