Math Core

Module 3.2 · Number Theory

Modular arithmetic

At the AMC 8 level, remainder problems are solved by spotting a repeating pattern. On the AMC 10 and 12 you need more power: congruence notation, division in modular arithmetic, Fermat's little theorem and Euler's theorem for huge exponents, and the Chinese remainder theorem for combining conditions. Together they turn "last two digits of 7777^{7^7}" into a two-line calculation.

Congruences

Definition

Congruence

a≡b(modm)a \equiv b \pmod{m} means mm divides a−ba - b, so aa and bb leave the same remainder when divided by mm.

Congruences behave like equations for addition, subtraction, multiplication and powers: if a≡ba \equiv b and c≡d(modm)c \equiv d \pmod{m}, then a+c≡b+da + c \equiv b + d, ac≡bdac \equiv bd, and an≡bn(modm)a^n \equiv b^n \pmod m. So you can replace any number by anything congruent to it, at any point. Negative representatives are especially handy: 5≡−1(mod6)5 \equiv -1 \pmod 6, so 5101≡(−1)101=−1≡5(mod6)5^{101} \equiv (-1)^{101} = -1 \equiv 5 \pmod 6.

Division needs care

You can't always divide. 2⋅3≡2⋅8(mod10)2 \cdot 3 \equiv 2 \cdot 8 \pmod{10}, but 3≢8(mod10)3 \not\equiv 8 \pmod{10}. Division by aa is allowed exactly when gcd⁡(a,m)=1\gcd(a, m) = 1. In that case aa has an inverse: a number a−1a^{-1} with a⋅a−1≡1(modm)a \cdot a^{-1} \equiv 1 \pmod m, and "dividing by aa" means multiplying by a−1a^{-1}.

To solve 7x≡5(mod12)7x \equiv 5 \pmod{12}: since 7⋅7=49≡17 \cdot 7 = 49 \equiv 1, the inverse of 77 is 77. Multiply both sides by 77: x≡35≡11(mod12)x \equiv 35 \equiv 11 \pmod{12}.

Huge exponents

Fermat and Euler

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.

Euler's theorem. If gcd⁡(a,m)=1\gcd(a, m) = 1, then aφ(m)≡1(modm)a^{\varphi(m)} \equiv 1 \pmod m, where φ(m)\varphi(m) counts the integers from 11 to mm that are relatively prime to mm. For m=p1a1⋯pkakm = p_1^{a_1} \cdots p_k^{a_k}, φ(m)=m(1−1p1)⋯(1−1pk)\varphi(m) = m\left(1 - \frac{1}{p_1}\right)\cdots\left(1 - \frac{1}{p_k}\right).

So to reduce an(modm)a^n \pmod m, reduce the exponent nn modulo φ(m)\varphi(m) (or modulo any smaller cycle length you find).

Why Fermat works: multiplying the nonzero residues 1,2,…,p−11, 2, \dots, p-1 by aa just rearranges them (no two products can be congruent, since you can cancel aa). So the product of all of them is unchanged: ap−1(p−1)!≡(p−1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod p, and cancelling (p−1)!(p-1)! gives ap−1≡1a^{p-1} \equiv 1. Euler's theorem is the same argument using only the residues relatively prime to mm.

Useful values: φ(100)=40\varphi(100) = 40, φ(1000)=400\varphi(1000) = 400. The actual cycle is often shorter. For instance 74=2401≡1(mod100)7^4 = 2401 \equiv 1 \pmod{100}, and 320≡1(mod100)3^{20} \equiv 1 \pmod{100}.

The Chinese remainder theorem

If mm and nn are relatively prime, then any pair of conditions x≡a(modm)x \equiv a \pmod m and x≡b(modn)x \equiv b \pmod n has exactly one solution modulo mnmn. In practice, you list numbers that satisfy the condition with the largest modulus and test them against the others. The theorem also works in reverse: to find something modulo 10001000, find it modulo 88 and modulo 125125 separately and combine.

Worked example: Fermat's little theorem

What is the remainder when 31003^{100} is divided by 77?

By Fermat, 36≡1(mod7)3^6 \equiv 1 \pmod 7. Since 100=6⋅16+4100 = 6 \cdot 16 + 4, 3100≡34=81≡4(mod7)3^{100} \equiv 3^4 = 81 \equiv 4 \pmod 7. The remainder is 44.

Worked example: Last two digits

What are the last two digits of 720267^{2026}?

72=497^2 = 49 and 74=2401≡1(mod100)7^4 = 2401 \equiv 1 \pmod{100}. Since 2026=4⋅506+22026 = 4 \cdot 506 + 2, 72026≡72=49(mod100)7^{2026} \equiv 7^2 = 49 \pmod{100}. The last two digits are 4949.

Worked example: Chinese remainder theorem

What is the smallest positive integer that leaves remainder 22 when divided by 33, remainder 33 when divided by 55, and remainder 44 when divided by 77?

Numbers that are 4(mod7)4 \pmod 7: 4,11,18,25,32,39,46,53,…4, 11, 18, 25, 32, 39, 46, 53, \dots Of these, 1818 and 5353 are 3(mod5)3 \pmod 5. Then 18≡0(mod3)18 \equiv 0 \pmod 3 fails and 53≡2(mod3)53 \equiv 2 \pmod 3 works. The answer is 5353, and every solution is 53+105k53 + 105k.

Worked example: A tower of exponents

What are the last two digits of 7777^{7^7}?

Since 74≡1(mod100)7^4 \equiv 1 \pmod{100}, you need the exponent 777^7 modulo 44. Because 7≡−1(mod4)7 \equiv -1 \pmod 4, 77≡(−1)7≡3(mod4)7^7 \equiv (-1)^7 \equiv 3 \pmod 4. So 777≡73=343≡43(mod100)7^{7^7} \equiv 7^3 = 343 \equiv 43 \pmod{100}. The last two digits are 4343.

Common mistake

Exponents are reduced modulo the cycle length (like p−1p - 1 or φ(m)\varphi(m)), never modulo mm itself. 210 mod 72^{10} \bmod 7 is not 23 mod 72^3 \bmod 7; since 23≡1(mod7)2^3 \equiv 1 \pmod 7, 210≡21=22^{10} \equiv 2^{1} = 2. And Fermat needs p∤ap \nmid a: 76≡0(mod7)7^{6} \equiv 0 \pmod 7, not 11.

Tip

When the modulus is not prime and the base shares a factor with it (like 22026 mod 10002^{2026} \bmod 1000), Euler's theorem doesn't apply directly. Split the modulus with the Chinese remainder theorem: the power is 0(mod8)0 \pmod 8, and Euler works modulo 125125.

Practice

Practice 1

What is the remainder when 220262^{2026} is divided by 77?

Practice 2

For how many integers nn with 1≤n≤1001 \le n \le 100 is n2+1n^2 + 1 divisible by 55?

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

Practice 3

What is the remainder when 13+23+33+⋯+10031^3 + 2^3 + 3^3 + \dots + 100^3 is divided by 77?

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

Practice 4

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

Practice 5

What are the last two digits of 320263^{2026}? (Enter them as a number.)

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

Practice 6

How many integers nn with 1≤n≤10001 \le n \le 1000 leave remainder 33 when divided by 44, remainder 22 when divided by 55, and remainder 55 when divided by 99?

Practice 7

What is the largest integer that divides n5−nn^5 - n for every integer nn?

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

Practice 8

What is the remainder when 12026+22026+32026+⋯+1220261^{2026} + 2^{2026} + 3^{2026} + \dots + 12^{2026} is divided by 1313?

Practice 9

What are the last two digits of 33333^{3^{3^3}}? (Enter them as a number.)

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

Real contest practice