Modular Arithmetic

Study Sheet

Modular Arithmetic

Congruences, inverses and linear congruences

Congruences

Concept
Working mod mm

a≡b(modm)a \equiv b \pmod m means m∣(a−b)m \mid (a - b): aa and bb leave the same remainder on division by mm. Congruences can be added, subtracted and multiplied like equations: if a≡ba \equiv b and c≡dc \equiv d then a+c≡b+da + c \equiv b + d and ac≡bdac \equiv bd. So you may reduce at every step of a computation — which is what makes 2100 mod 72^{100} \bmod 7 easy.

Reducing mod mm is forgetting everything about a number except its remainder, and the miracle is that arithmetic still works on remainders alone: if a=b+mka = b + mk and c=d+mlc = d + ml, then ac=bd+m(bl+dk+mkl)ac = bd + m(bl + dk + mkl), so the products differ by a multiple of mm too. Picture the residues 0,1,…,m−10, 1, \ldots, m - 1 around a clock; adding moves around it, and whole laps do not matter. The one operation that needs care is division, taken up in the third section.

Example
Days of the week
TueWedThuFriSatSunMon100 = 14·7 + 2: 14 full laps, then 2 steps
Only the remainder 2 moves the day; the 14 laps change nothing.

Today is Tuesday. What day is it 100100 days from now? Days repeat every 77, so only 100 mod 7100 \bmod 7 matters: 100=14⋅7+2100 = 14\cdot7 + 2. Fourteen whole weeks bring us back to Tuesday, and 22 more days reach Thursday.

Example
Big powers

Find 2100 mod 72^{100} \bmod 7. Look for a small power that is ≡1\equiv 1: 23=8≡1(mod7)2^3 = 8 \equiv 1 \pmod 7. Then write the exponent in multiples of 33: 2100=(23)33⋅2≡133⋅2=22^{100} = (2^3)^{33}\cdot2 \equiv 1^{33}\cdot2 = 2. Multiplying congruences is what licenses replacing 232^3 by 11 inside the power. Answer 22.

Example
Two powers at once

Find the remainder of 350+5503^{50} + 5^{50} on division by 88. Squares first: 32=9≡13^2 = 9 \equiv 1 and 52=25≡1(mod8)5^2 = 25 \equiv 1 \pmod 8. So 350=(32)25≡13^{50} = (3^2)^{25} \equiv 1 and 550=(52)25≡15^{50} = (5^2)^{25} \equiv 1, and the sum is ≡2\equiv 2. The remainder is 22 — no need to know that 3503^{50} has 2424 digits.

Side note
Where it goes wrong

Reducing an EXPONENT mod mm. It is tempting to say 210≡23=8≡1(mod7)2^{10} \equiv 2^{3} = 8 \equiv 1 \pmod 7 because 10≡310 \equiv 3; but 210=1024=146⋅7+2≡22^{10} = 1024 = 146\cdot7 + 2 \equiv 2. Bases may be reduced mod mm; exponents may only be reduced mod the length of the cycle of powers (here 33, since 23≡12^3 \equiv 1), and 10≡1(mod3)10 \equiv 1 \pmod 3 gives the correct 210≡22^{10} \equiv 2.

Try it
Try it: last digit

Find the last digit of 720257^{2025}.

Show the answer

The last digit is the value mod 1010. Powers of 77 mod 1010 run 7,9,3,17, 9, 3, 1 and then repeat, since 74≡17^4 \equiv 1. As 2025=4⋅506+12025 = 4\cdot506 + 1, 72025≡717^{2025} \equiv 7^1: the last digit is 7\mathbf{7}.

Proofs by Residue Classes

Concept
Check every class

To prove a statement about all integers nn whose truth depends only on n mod mn \bmod m, check the mm residues 0,1,…,m−10, 1, \ldots, m - 1. Every integer is congruent to exactly one of them, so mm checks cover infinitely many cases. Standard facts proved this way: squares are 00 or 1(mod4)1 \pmod 4; squares are 0,1,4(mod5)0, 1, 4 \pmod 5; squares are 00 or 1(mod3)1 \pmod 3.

Why is checking finitely many cases enough? Because if n≡rn \equiv r then n2≡r2n^2 \equiv r^2, n3−n≡r3−rn^3 - n \equiv r^3 - r, and so on for any polynomial with integer coefficients: the value mod mm is decided by the residue class. So the infinite set of integers collapses into mm representatives, and a finite table becomes a proof. Choose mm to match the claim — divisibility by 66 suggests checking mod 22 and mod 33.

Example
Squares mod 55
01234n → n² mod 5 lands only on 0, 1, 4
Arrows send each residue to its square; 2 and 3 are never hit, so n² + 2 is never 0.

Show that n2+2n^2 + 2 is never divisible by 55. Square each residue: 0→00 \to 0, 1→11 \to 1, 2→42 \to 4, 3→9≡43 \to 9 \equiv 4, 4→16≡14 \to 16 \equiv 1. So n2∈{0,1,4}n^2 \in \{0, 1, 4\} mod 55 and n2+2∈{2,3,1}n^2 + 2 \in \{2, 3, 1\} — never 00. Notice that 22 and 3=−23 = -2 land together, as do 11 and 4=−14 = -1: nn and −n-n always have the same square.

Example
n3−nn^3 - n is divisible by 66

Since 6=2⋅36 = 2\cdot3 with gcd⁡(2,3)=1\gcd(2, 3) = 1, it suffices to show 2∣n3−n2 \mid n^3 - n and 3∣n3−n3 \mid n^3 - n. Mod 22: 03−0=00^3 - 0 = 0 and 13−1=01^3 - 1 = 0. Mod 33: 0−0=00 - 0 = 0, 1−1=01 - 1 = 0, 8−2=6≡08 - 2 = 6 \equiv 0. Every class gives 00, so both primes divide n3−nn^3 - n, and so does 66. (Factored, n3−n=(n−1)n(n+1)n^3 - n = (n - 1)n(n + 1) is a product of three consecutive integers.)

Example
A fact about right triangles

If a2+b2=c2a^2 + b^2 = c^2 in integers, show that 33 divides aa or bb. Squares mod 33 are 00 (for n≡0n \equiv 0) or 11 (for n≡±1n \equiv \pm1). If neither aa nor bb were a multiple of 33, then a2+b2≡1+1=2a^2 + b^2 \equiv 1 + 1 = 2, but c2c^2 can only be 00 or 11. So one of a,ba, b is a multiple of 33 — as in 3,4,53, 4, 5 and 5,12,135, 12, 13.

Side note
Where it goes wrong

Checking a few values of nn instead of every residue class. n2+n+41n^2 + n + 41 is prime for n=0,1,…,39n = 0, 1, \ldots, 39 — forty successes in a row — yet at n=40n = 40 it equals 1681=4121681 = 41^2. Examples suggest; only a complete case check (or an algebraic argument) proves.

Try it
Try it: never a multiple of 33

Prove that n2+1n^2 + 1 is never divisible by 33.

Show the answer

Check the three classes mod 33: n≡0,1,2n \equiv 0, 1, 2 give n2≡0,1,4≡1n^2 \equiv 0, 1, 4 \equiv 1, so n2+1≡1,2,2n^2 + 1 \equiv 1, 2, 2. None is 00, so 3∤n2+13 \nmid n^2 + 1 for every integer nn.

Inverses and Linear Congruences

Concept
Modular inverses

aa has an inverse mod mm (a number a−1a^{-1} with aa−1≡1aa^{-1} \equiv 1) exactly when gcd⁡(a,m)=1\gcd(a, m) = 1; find it from Bézout. The congruence ax≡b(modm)ax \equiv b \pmod m has a solution iff gcd⁡(a,m)∣b\gcd(a, m) \mid b, and then exactly gcd⁡(a,m)\gcd(a, m) solutions mod mm.

Solving ax≡b(modm)ax \equiv b \pmod m is solving ax−my=bax - my = b in integers, and by Bézout that is possible exactly when d=gcd⁡(a,m)d = \gcd(a, m) divides bb. When d=1d = 1, multiplying by a−1a^{-1} undoes multiplication by aa, just as division does for ordinary equations. When d>1d > 1, multiplication by aa is many-to-one: it squeezes the mm residues onto only the multiples of dd, each hit dd times — which is where the dd solutions come from.

Tip
Cancellation

If ac≡bc(modm)ac \equiv bc \pmod m and gcd⁡(c,m)=1\gcd(c, m) = 1, then a≡b(modm)a \equiv b \pmod m. If gcd⁡(c,m)=d\gcd(c, m) = d, you can only conclude a≡b(modm/d)a \equiv b \pmod{m/d}.

Example
Solve 7x≡3(mod20)7x \equiv 3 \pmod{20}

gcd⁡(7,20)=1\gcd(7, 20) = 1, so 77 has an inverse and there is exactly one solution. Find the inverse by trying multiples of 77 until one is 11 more than a multiple of 2020: 7⋅3=21≡17\cdot3 = 21 \equiv 1, so 7−1≡37^{-1} \equiv 3. Multiply both sides by 33: x≡3⋅3=9x \equiv 3\cdot3 = 9. Check: 7⋅9=63=3⋅20+37\cdot9 = 63 = 3\cdot20 + 3. ✓

Example
Solve 6x≡4(mod10)6x \equiv 4 \pmod{10}
00112233445566778899x6xx = 4 and x = 9 both land on 4; odd residues are never hit
Multiplying by 6 mod 10 hits each even residue twice and odd ones never.

gcd⁡(6,10)=2\gcd(6, 10) = 2 divides 44, so there are 22 solutions. Divide everything, modulus included, by 22: 3x≡2(mod5)3x \equiv 2 \pmod 5. Now 3−1≡23^{-1} \equiv 2 (as 3⋅2=6≡13\cdot2 = 6 \equiv 1), so x≡4(mod5)x \equiv 4 \pmod 5. Back mod 1010 that is x=4x = 4 or x=9x = 9. The figure shows why: 6x mod 106x \bmod 10 only ever lands on even residues, each one twice.

Example
Solve 12x≡9(mod21)12x \equiv 9 \pmod{21}

gcd⁡(12,21)=3\gcd(12, 21) = 3 and 3∣93 \mid 9: three solutions. Divide by 33: 4x≡3(mod7)4x \equiv 3 \pmod 7. Since 4⋅2=8≡14\cdot2 = 8 \equiv 1, 4−1≡24^{-1} \equiv 2 and x≡6(mod7)x \equiv 6 \pmod 7. Mod 2121 the solutions are x=6,13,20x = 6, 13, 20. Check x=13x = 13: 156=7⋅21+9156 = 7\cdot21 + 9. ✓ By contrast 10x≡5(mod14)10x \equiv 5 \pmod{14} has NO solution: gcd⁡(10,14)=2\gcd(10, 14) = 2 does not divide 55 (an even number cannot be 55 more than a multiple of 1414).

Side note
Where it goes wrong

Dividing both sides of a congruence by a number that shares a factor with the modulus. 2x≡2(mod4)2x \equiv 2 \pmod 4 does NOT give x≡1x \equiv 1; x=3x = 3 also works (6≡26 \equiv 2). Dividing by 22 is allowed only if the modulus is divided too: x≡1(mod2)x \equiv 1 \pmod 2.

Try it
Try it: inverse

Find the inverse of 1111 modulo 2626.

Show the answer

26=2⋅11+426 = 2\cdot11 + 4; 11=2⋅4+311 = 2\cdot4 + 3; 4=3+14 = 3 + 1. Back: 1=4−3=4−(11−2⋅4)=3⋅4−11=3(26−2⋅11)−11=3⋅26−7⋅111 = 4 - 3 = 4 - (11 - 2\cdot4) = 3\cdot4 - 11 = 3(26 - 2\cdot11) - 11 = 3\cdot26 - 7\cdot11. So 11−1≡−7≡1911^{-1} \equiv -7 \equiv \mathbf{19}. Check: 11⋅19=209=8⋅26+111\cdot19 = 209 = 8\cdot26 + 1. ✓

Why It Works

If ax≡1(modm)ax \equiv 1 \pmod m then ax−my=1ax - my = 1 for some yy, so any common divisor of aa and mm divides 11: gcd⁡(a,m)=1\gcd(a, m) = 1. Conversely, if gcd⁡(a,m)=1\gcd(a, m) = 1, Bézout gives ax+my=1ax + my = 1, and reducing mod mm leaves ax≡1ax \equiv 1. The residues coprime to mm therefore form a group under multiplication — closed, with identity 11, every element invertible — and the size of that group is Euler's φ(m)\varphi(m), which is where the Fermat–Euler topic begins.