Modular Arithmetic
Modular Arithmetic: Residue Classes, Inverses, and Linear Congruences
Congruence and residue classes
Fix a positive integer n, the modulus. Two integers are congruent modulo n, written a ≡ b (mod n), when n divides a − b. Equivalently, a and b leave the same remainder on division by n.
Congruence is an equivalence relation, so it splits the integers into residue classes. The class of a is [a] = {…, a − 2n, a − n, a, a + n, …}. There are exactly n classes, represented by the least nonnegative residues 0, 1, …, n − 1. The set of classes is written Z/nZ (the integers modulo n).
Negative numbers. The division algorithm gives a unique q and r with a = qn + r and 0 ≤ r < n, even for negative a.
For −23 modulo 6, write −23 = 6 × (−4) + 1, so the least nonnegative residue is 1. A quick method: add multiples of 6 until the result lands in 0–5 (−23 + 24 = 1).
Many programming languages return −5 for −23 % 6; that value is congruent to 1 but is not the least nonnegative residue.
Arithmetic on classes
If a ≡ a′ and b ≡ b′ (mod n), then
- a + b ≡ a′ + b′,
- a − b ≡ a′ − b′,
- ab ≡ a′b′ (mod n).
The proof of the product rule shows why: ab − a′b′ = a(b − b′) + b′(a − a′), a sum of multiples of n. These rules make addition and multiplication of classes well defined, and they justify reducing early: replace each number by a small representative before computing.
Worked example. Find 38 × 54 modulo 7. Since 38 ≡ 3 and 54 ≡ 5, the product is ≡ 15 ≡ 1 (mod 7). Directly, 38 × 54 = 2052 = 7 × 293 + 1, which agrees.
Powers by repeated squaring. To find 210 modulo 13, square and reduce: 22 = 4, 24 = 16 ≡ 3, 28 ≡ 9. Then 210 = 28 × 22 ≡ 9 × 4 = 36 ≡ 10 (mod 13). Only bases are reduced modulo n; exponents are ordinary counts and must not be reduced modulo n.
Division is different: units and zero divisors
In ordinary arithmetic you can cancel a nonzero factor. Modulo n that can fail. Modulo 12, 4 × 5 = 20 ≡ 8 and 4 × 2 = 8, so 4 × 5 ≡ 4 × 2 even though 5 ≢ 2. Also 4 × 3 = 12 ≡ 0 with neither factor ≡ 0; such classes are zero divisors.
The fix is to replace division by multiplication by an inverse. A class [a] has an inverse [a]−1 with a × a−1 ≡ 1 (mod n) exactly when gcd(a, n) = 1. Such classes are called units. When n is prime, every nonzero class is a unit.
Finding an inverse with the extended Euclidean algorithm.
For 7 modulo 19, run the Euclidean algorithm: 19 = 2 × 7 + 5, 7 = 1 × 5 + 2, 5 = 2 × 2 + 1. Back-substitute: 1 = 5 − 2 × 2 = 5 − 2(7 − 5) = 3 × 5 − 2 × 7 = 3(19 − 2 × 7) − 2 × 7 = 3 × 19 − 8 × 7. Reducing modulo 19 gives −8 × 7 ≡ 1, so 7−1 ≡ −8 ≡ 11. Check: 7 × 11 = 77 = 4 × 19 + 1.
Solving linear congruences
To solve ax ≡ b (mod n), let d = gcd(a, n).
- Solvability. A solution exists if and only if d divides b. (If ax − b = kn, then d divides ax − kn = b.)
- If d = 1: multiply both sides by a−1. The solution is unique modulo n.
- If d > 1 and d divides b: divide a, b, and n by d, solve the reduced congruence (whose coefficient is now a unit), and lift. The reduced solution x0 modulo n/d gives exactly d solutions modulo n: x0, x0 + n/d, …, x0 + (d − 1)n/d.
Worked example 1. Solve 7x ≡ 3 (mod 19). Here gcd(7, 19) = 1 and 7−1 ≡ 11, so x ≡ 11 × 3 = 33 ≡ 14 (mod 19). Check: 7 × 14 = 98 = 5 × 19 + 3.
Worked example 2. Solve 9x ≡ 12 (mod 21). Here d = gcd(9, 21) = 3, which divides 12, so there are 3 solutions modulo 21. Divide everything by 3: 3x ≡ 4 (mod 7).
The inverse of 3 modulo 7 is 5 (since 15 ≡ 1), so x ≡ 20 ≡ 6 (mod 7). Lifting to modulo 21 gives x ≡ 6, 13, 20 (mod 21). Check one: 9 × 13 = 117 = 5 × 21 + 12.
The key error to avoid. Dividing a and b by d but keeping the original modulus gives the wrong congruence.
In example 2, “3x ≡ 4 (mod 21)” has no solutions at all (gcd(3, 21) = 3 does not divide 4), even though the original congruence has three, because the modulus was not divided as well. Always divide all three numbers.
Checks that catch most mistakes
- Substitute each claimed solution into the original congruence.
- Confirm that the number of solutions modulo n equals d (or zero when d does not divide b).
- Report answers as residue classes (“x ≡ … (mod n)”) or list every integer in a requested range.
Special results for prime moduli and factorials, and the simultaneous solution of congruences with different moduli, belong to the sibling concepts on Fermat's and Wilson's theorems and Sun-tzu's theorem.
Mark Yourself Question
- Grab a piece of paper and try this problem yourself.
- When you're done, check the "I have answered this question" box below.
- View the solution and report whether you got it right or wrong.
Without computing the full products, find the remainder when 47 × 86 − 592 is divided by 9. Give the least nonnegative residue.
Mark Yourself Question
- Grab a piece of paper and try this problem yourself.
- When you're done, check the "I have answered this question" box below.
- View the solution and report whether you got it right or wrong.
Solve 17x ≡ 5 (mod 43). Give the solution as a least nonnegative residue modulo 43.