Modular Arithmetic
Modular arithmetic is arithmetic with remainders. Two integers a and b are congruent modulo n, written a ≡ b (mod n), when n divides a − b, which means they leave the same remainder when divided by n. You can add, subtract, multiply and take powers of congruences, reducing mod n at any step.
Before this lesson: Divisibility and the Division Algorithm and Euclidean Algorithm
Congruence modulo n
A 12-hour clock already does modular arithmetic: 7 hours after 9 o’clock is 4 o’clock, because 9 + 7 = 16 and 16 leaves remainder 4 when divided by 12. Modular arithmetic keeps only the remainder and forgets the rest.
a \equiv b \pmod{n} \iff n \mid (a - b)Here n ≥ 2 is the modulus. Read a ≡ b (mod n) as “a is congruent to b mod n.” By the division algorithm, every integer is congruent to exactly one of 0, 1, …, n − 1, its remainder a mod n. So a ≡ b (mod n) exactly when a mod n = b mod n.
Note the two uses of “mod.” In a ≡ b (mod n), it names the setting of a statement that is true or false. In a mod n, it is an operation that outputs a single remainder, such as 16 mod 12 = 4.
Rules of modular arithmetic
Suppose a ≡ b (mod n) and c ≡ d (mod n). Then the following hold for every positive integer k.
a + c \equiv b + d, \quad a - c \equiv b - d, \quad ac \equiv bd, \quad a^k \equiv b^k \pmod{n}In practice, this means you may replace any number by its remainder at any step, which keeps the numbers small. Here is the proof of the multiplication rule.
n \mid (a - b) \text{ and } n \mid (c - d)Write both hypotheses with the definition.
ac - bd = ac - ad + ad - bd
Subtract and add ad.
ac - bd = a(c - d) + d(a - b)
Factor each pair of terms (see factoring by grouping).
n \mid ac - bd
The right side is a combination of two multiples of n.
ac \equiv bd \pmod{n}This is the definition of congruence.
Applying the multiplication rule k times gives the power rule. Division is the exception: 2 · 3 ≡ 2 · 8 (mod 10), since 6 and 16 both leave remainder 6, but 3 ≢ 8 (mod 10). Canceling a factor c is safe only when gcd(c, n) = 1, which the next lesson handles.
Modular arithmetic examples
Example 1: Testing congruences
Decide whether 47 ≡ 12 (mod 7) and whether 29 ≡ −6 (mod 8).
47 - 12 = 35
Subtract to apply the definition.
35 = 7 \cdot 5
35 is a multiple of 7.
47 \equiv 12 \pmod{7}So the congruence is true.
29 - (-6) = 35
Subtract. Subtracting a negative adds its opposite.
8 \cdot 4 = 32 \lt 35 \lt 40 = 8 \cdot 5
35 lies strictly between consecutive multiples of 8.
29 \not\equiv -6 \pmod{8}So 8 does not divide the difference.
Answer: 47 ≡ 12 (mod 7) is true, since both leave remainder 5. 29 ≡ −6 (mod 8) is false, since 29 mod 8 = 5 and −6 mod 8 = 2.
Example 2: Reduce first, then compute
Find (1234 + 5678) mod 9 and (1234 · 5678) mod 9 without adding or multiplying the large numbers.
1234 = 9 \cdot 137 + 1
Divide 1234 by 9: 9 · 137 = 1233.
1234 \equiv 1 \pmod{9}Replace 1234 by its remainder.
5678 = 9 \cdot 630 + 8
Divide 5678 by 9: 9 · 630 = 5670.
5678 \equiv 8 \pmod{9}Replace 5678 by its remainder.
1234 + 5678 \equiv 1 + 8 = 9
Use the addition rule.
9 \equiv 0 \pmod{9}Reduce: 9 leaves remainder 0.
1234 \cdot 5678 \equiv 1 \cdot 8 = 8 \pmod{9}Use the multiplication rule.
Answer: (1234 + 5678) mod 9 = 0 and (1234 · 5678) mod 9 = 8. Checking directly, 6912 = 9 · 768 and 7,006,652 = 9 · 778,516 + 8.
Example 3: Negative numbers
Find −23 mod 6, then find (−23 · 17) mod 6.
-23 = 6(-4) + 1
Use the division algorithm. The quotient −4 is ⌊−23/6⌋, and the remainder must be at least 0.
-23 \equiv 1 \pmod{6}So −23 mod 6 = 1.
17 = 6 \cdot 2 + 5
Divide 17 by 6.
17 \equiv 5 \pmod{6}So 17 mod 6 = 5.
-23 \cdot 17 \equiv 1 \cdot 5 = 5 \pmod{6}Use the multiplication rule.
Answer: −23 mod 6 = 1, and (−23 · 17) mod 6 = 5. Check: −23 · 17 = −391 = 6(−66) + 5.
Example 4: A large power by repeated squaring
Find 345 mod 17. The number 345 has 22 digits, so reduce after every multiplication instead.
3^2 = 9
Start squaring.
3^4 = 9^2 = 81 \equiv 13
Square again, then reduce: 81 − 4 · 17 = 13.
3^8 \equiv 13^2 = 169 \equiv 16
Square, then reduce: 169 − 9 · 17 = 16.
3^{16} \equiv 16^2 = 256 \equiv 1Square, then reduce: 256 − 15 · 17 = 1.
3^{32} \equiv 1^2 = 1Square once more.
45 = 32 + 8 + 4 + 1
Write the exponent as a sum of powers of 2 (its binary form, 101101).
3^{45} = 3^{32} \cdot 3^8 \cdot 3^4 \cdot 3^1Split the power (see exponent rules).
3^{45} \equiv 1 \cdot 16 \cdot 13 \cdot 3Substitute the reduced squares. All congruences here are mod 17.
16 \cdot 13 = 208 \equiv 4
Multiply, then reduce: 208 − 12 · 17 = 4.
4 \cdot 3 = 12
Multiply the last factor.
Answer: 345 mod 17 = 12. This method, called repeated squaring or fast modular exponentiation, needs only about twice as many multiplications as the exponent has binary digits.
Example 5: A 24-hour clock (word problem)
A 24-hour clock shows 09:00. What will it show 1,000 hours from now?
\dfrac{1000}{24} \approx 41.67The clock repeats every 24 hours, so work mod 24.
24 \cdot 41 = 984
Multiply by the floor of the quotient.
1000 - 984 = 16
Subtract to get the remainder.
1000 \equiv 16 \pmod{24}So 1,000 hours moves the clock the same as 16 hours.
9 + 16 = 25
Add the hours to the start time.
25 \equiv 1 \pmod{24}Reduce: 25 − 24 = 1.
Answer: the clock will show 01:00, about 41 days and 16 hours later.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Reading ≡ as equality | “17 ≡ 5 (mod 12), so 17 = 5.” | Congruent numbers differ by a multiple of n: 17 − 5 = 12. |
| Giving a negative remainder | −23 mod 6 = −5 | a mod n is between 0 and n − 1: −23 = 6(−4) + 1, so the answer is 1. |
| Canceling a common factor | 6 ≡ 16 (mod 10), so 3 ≡ 8 (mod 10) | Cancel c only when gcd(c, n) = 1. Here gcd(2, 10) = 2. |
| Reducing the exponent mod n | 213 ≡ 23 ≡ 3 (mod 5) | Exponents do not reduce mod n. In fact 213 = 8192 ≡ 2 (mod 5). |
| Computing the full power first | Finding all 22 digits of 345 before dividing | Reduce after every multiplication, using repeated squaring. |
| Using n = 1 or n = 0 | “5 ≡ 2 (mod 1)” | Every pair of integers is congruent mod 1, so take n ≥ 2. |
Related ideas
Congruence mod n is an equivalence relation, and its equivalence classes are the residue classes [0], [1], …, [n − 1], often written ℤn. The lesson Equivalence Relations develops that idea in general.
The next lesson, Linear Congruences, solves equations like 7x ≡ 3 (mod 10) with modular inverses from the extended Euclidean algorithm. Fermat’s Little Theorem explains when exponents can be reduced, and RSA encryption is built on repeated squaring.
Practice problems
- Is 85 ≡ 13 (mod 9)?
- Is −17 ≡ 25 (mod 6)?
- Find 2026 mod 11.
- Find −45 mod 8.
- Find (387 + 945) mod 4 by reducing first.
- Find (386 · 497) mod 5 by reducing first.
- Use repeated squaring to find 220 mod 13.
- Find the last digit of 72026.
- Show that 10 ≡ 1 (mod 9), and use it to find 5832 mod 9 from its digits.
- Today is Saturday. What day of the week was it 100 days ago?
Show answers
1.
85 - 13 = 72
Subtract.
72 = 9 \cdot 8
72 is a multiple of 9.
Answer: yes, 85 ≡ 13 (mod 9)
2.
-17 - 25 = -42
Subtract.
-42 = 6(-7)
−42 is a multiple of 6.
Answer: yes, −17 ≡ 25 (mod 6)
3.
11 \cdot 184 = 2024
Find the multiple of 11 just below 2026.
2026 - 2024 = 2
Subtract to get the remainder.
Answer: 2026 mod 11 = 2
4.
\dfrac{-45}{8} = -5.625Divide.
q = \lfloor -5.625 \rfloor = -6
Round down.
8 \cdot (-6) = -48
Multiply the modulus by the quotient.
-45 - (-48) = 3
Subtract to get the remainder.
Answer: −45 mod 8 = 3
5.
387 = 4 \cdot 96 + 3
Reduce 387 mod 4: 4 · 96 = 384.
945 = 4 \cdot 236 + 1
Reduce 945 mod 4: 4 · 236 = 944.
3 + 1 = 4 \equiv 0 \pmod{4}Add the remainders and reduce.
Answer: (387 + 945) mod 4 = 0. Indeed 1332 = 4 · 333
6.
386 \equiv 1 \pmod{5}386 = 5 · 77 + 1.
497 \equiv 2 \pmod{5}497 = 5 · 99 + 2.
1 \cdot 2 = 2
Multiply the remainders.
Answer: (386 · 497) mod 5 = 2
7.
2^4 = 16 \equiv 3
Reduce mod 13: 16 − 13 = 3.
2^8 \equiv 3^2 = 9
Square.
2^{16} \equiv 9^2 = 81 \equiv 3Square, then reduce: 81 − 6 · 13 = 3.
2^{20} = 2^{16} \cdot 2^4Split the power, since 20 = 16 + 4.
2^{20} \equiv 3 \cdot 3 = 9Multiply the reduced factors.
Answer: 220 mod 13 = 9
8.
7^1 \equiv 7 \pmod{10}The last digit is the remainder mod 10.
7^2 = 49 \equiv 9
Multiply by 7, then reduce mod 10.
7^3 \equiv 9 \cdot 7 = 63 \equiv 3
Multiply by 7, then reduce.
7^4 \equiv 3 \cdot 7 = 21 \equiv 1
Multiply by 7, then reduce.
2026 = 4 \cdot 506 + 2
The pattern repeats every 4, so divide the exponent by 4.
7^{2026} = (7^4)^{506} \cdot 7^2Split the power.
7^{2026} \equiv 1^{506} \cdot 9 = 9Substitute 74 ≡ 1 and 72 ≡ 9.
Answer: the last digit of 72026 is 9
9.
10 - 1 = 9
9 divides 10 − 1, so 10 ≡ 1 (mod 9).
10^k \equiv 1^k = 1
Use the power rule.
5832 = 5 \cdot 10^3 + 8 \cdot 10^2 + 3 \cdot 10 + 2
Write the number by place value.
5832 \equiv 5 + 8 + 3 + 2 = 18
Replace each power of 10 by 1.
18 \equiv 0 \pmod{9}Reduce.
Answer: 5832 mod 9 = 0. A number and its digit sum always leave the same remainder mod 9
10.
-100 = 7(-15) + 5
Find −100 mod 7: 7 · (−15) = −105.
-100 \equiv 5 \pmod{7}Going back 100 days is the same as going forward 5 days.
\text{Sat} \to \text{Sun} \to \text{Mon} \to \text{Tue} \to \text{Wed} \to \text{Thu}Count forward 5 days.
Answer: Thursday
Modular arithmetic FAQ
What does a ≡ b (mod n) mean?
It means n divides a − b. Equivalently, a and b leave the same remainder when divided by n. For example, 38 ≡ 2 (mod 12) because 38 − 2 = 36 is a multiple of 12.
How do you find a negative number mod n?
Write it with the division algorithm, rounding the quotient down so the remainder is between 0 and n − 1. For example, −23 = 6(−4) + 1, so −23 mod 6 = 1. Adding n to a negative remainder gives the same answer.
Can you divide in modular arithmetic?
Not in general. You can cancel a factor c from both sides only when gcd(c, n) = 1, and then dividing by c means multiplying by its modular inverse.
What is modular arithmetic used for?
It is used in clocks and calendars, check digits on ISBNs and credit cards, hashing in computer science and public-key encryption such as RSA.
Summary
- a ≡ b (mod n) means n divides a − b, or equivalently that a and b have the same remainder mod n.
- a mod n is the remainder from the division algorithm, always between 0 and n − 1, even for negative a.
- Congruences can be added, subtracted, multiplied and raised to powers, so reduce mod n at any step.
- Find large powers by repeated squaring, reducing after every multiplication.
- Do not cancel factors or reduce exponents mod n without a reason.
Previous lesson: Euclidean Algorithm
Next lesson: Linear Congruences
Back to the Discrete Math Index