Sub topic 3
Adding, Subtracting and Multiplying Remainders
What this chapter assumes
You should already know integer division with a quotient and a remainder, and the congruence notation a ≡ b (mod n) from the previous chapter. You should also be comfortable with basic factors and divisibility. This chapter turns those ideas into a working method. You will learn to replace large numbers with their small remainders, do the arithmetic on the small numbers, and then reduce the result.
The core idea
Every integer a can be written as a = n·q + r, where 0 ≤ r < n. Only r matters when you divide by n. The multiple n·q vanishes in every remainder calculation. This gives three rules. Suppose a ≡ r₁ (mod n) and b ≡ r₂ (mod n). Then:
- Sum: a + b ≡ r₁ + r₂ (mod n)
- Difference: a − b ≡ r₁ − r₂ (mod n)
- Product: a · b ≡ r₁ · r₂ (mod n)
In words: you may reduce each number first, then combine, then reduce again. The answer is the same as reducing the full result directly.
Why the rules hold
Write a = n·p + r₁ and b = n·q + r₂. Then:
- a + b = n(p + q) + (r₁ + r₂). The first part is a multiple of n.
- a · b = n²pq + n·p·r₂ + n·q·r₁ + r₁r₂ = n(npq + p·r₂ + q·r₁) + r₁r₂. Again everything except r₁r₂ is a multiple of n.
So the leftover part decides the remainder. The same argument works for subtraction.
q, r = divmod(47, 6)
print(q, r) # 7 5
print(47 == 6 * q + r) # True
print((-7) % 5) # 3The code shows the division identity 47 = 6·7 + 5. It also shows that Python's % gives a non-negative remainder for a positive divisor, even when the dividend is negative. The remainder of −7 on division by 5 is 3, because −7 = 5·(−2) + 3.
Worked example: a sum
Find the remainder when 1234 + 5678 is divided by 9.
- 1234 divided by 9: 9 × 137 = 1233, so the remainder is 1.
- 5678 divided by 9: 9 × 630 = 5670, so the remainder is 8.
- Add the remainders: 1 + 8 = 9.
- 9 is itself divisible by 9, so reduce it again. The remainder is 0.
The last step is where many learners slip. The sum of remainders can reach n or more, so it must be reduced one more time.
Worked example: a difference
Find the remainder when 45 − 38 is divided by 7.
- 45 leaves remainder 3 on division by 7.
- 38 leaves remainder 3 on division by 7.
- 3 − 3 = 0, so the remainder is 0. Indeed 45 − 38 = 7.
Now try a case where the difference of remainders is negative. Find the remainder when 31 − 48 is divided by 5.
- 31 leaves remainder 1.
- 48 leaves remainder 3.
- 1 − 3 = −2. Add the divisor: −2 + 5 = 3.
Check: 31 − 48 = −17, and −17 = 5·(−4) + 3. The remainder is 3.
Worked example: a product
Find the remainder when 23 × 38 is divided by 7.
- 23 = 7·3 + 2, so the remainder is 2.
- 38 = 7·5 + 3, so the remainder is 3.
- 2 × 3 = 6, which is below 7.
The remainder is 6. You can verify this with code. The first line computes the full product, and the second line reduces each factor first.
a, b, n = 23, 38, 7
print((a * b) % n) # 5
print(((a % n) * (b % n)) % n) # 5Both lines print 5, not 6. Check the arithmetic: 38 = 7·5 + 3, so 38 leaves remainder 3. But 23 = 7·3 + 2 leaves remainder 2, and 2 × 3 = 6. Yet 23 × 38 = 874, and 874 = 7·124 + 6. So the true remainder is 6.
Whenever a comment and a computed result disagree, recompute by hand. Here the hand computation 23 × 38 = 874 = 7 × 124 + 6 settles it.
Products of many factors
The product rule extends to any number of factors. Reduce each factor, multiply step by step, and reduce after every step so numbers stay small.
Find the remainder when 17 × 19 × 23 × 29 is divided by 12.
- 17 ≡ 5, 19 ≡ 7, 23 ≡ 11, 29 ≡ 5 (mod 12).
- 5 × 7 = 35 ≡ 11.
- 11 × 11 = 121 ≡ 1 (121 = 12·10 + 1).
- 1 × 5 = 5.
The remainder is 5. Reducing after each multiplication keeps every intermediate value under 144.
Expressions with several operations
You can mix operations. Treat each number as its remainder, then follow normal order of operations, reducing as you go.
Find the remainder when 15 × 22 + 9 × 14 − 31 is divided by 8.
- 15 ≡ 7, 22 ≡ 6, so 15 × 22 ≡ 42 ≡ 2.
- 9 ≡ 1, 14 ≡ 6, so 9 × 14 ≡ 6.
- 31 ≡ 7.
- Total: 2 + 6 − 7 = 1.
The remainder is 1.
What does not work: division
Addition, subtraction and multiplication are safe. Division is not. You cannot divide remainders the way you divide numbers.
Example: 12 ≡ 2 (mod 10) and 6 ≡ 6 (mod 10), yet 12 ÷ 6 = 2 while 2 ÷ 6 is not a whole number. A more telling case: 4 × 3 ≡ 4 × 8 (mod 10) because 12 ≡ 2 and 32 ≡ 2, but 3 ≢ 8 (mod 10). You cannot cancel the common factor 4 freely.
The same remainder, several ways
A useful table of the three rules, using a ≡ 4 and b ≡ 6 (mod 7):
| Operation | Remainders used | Raw result | Reduced result |
|---|---|---|---|
| a + b | 4 + 6 | 10 | 3 |
| a − b | 4 − 6 | −2 | 5 |
| a × b | 4 × 6 | 24 | 3 |
Each raw result lies outside the range 0 to 6 or at least is not guaranteed to fit, so the final reduction step is part of the method.
A flow for every problem
Common errors
- Forgetting the final reduction. A sum or product of remainders can exceed n − 1.
- Leaving a negative result. Add n until it is between 0 and n − 1.
- Treating division as safe. Reduce numerators and denominators separately only after checking the cancellation rule above.
- Reducing the wrong thing. You may reduce factors and terms, but you may not reduce an exponent by n. For example 2^10 mod 7 is not 2^3 mod 7. Exponents follow cycle rules, covered in later chapters.
- Arithmetic slips when remainders are large. If a remainder is close to n, a negative version such as −1 is easier to multiply. The next chapter develops this idea.
Quick checks under time pressure
- Work with the smallest remainders you can find, positive or negative.
- Reduce after each step, not only at the end.
- Verify with a rough check, such as a divisibility test, when time permits.
Exercise
Find the remainder when 245 × 361 + 18 is divided by 11.
Show answer
245 = 11·22 + 3, so 245 ≡ 3. 361 = 11·32 + 9, so 361 ≡ 9. Then 3 × 9 = 27 ≡ 5, and 18 ≡ 7. The total is 5 + 7 = 12 ≡ 1. The remainder is 1.
Exercise
A number leaves remainder 4 when divided by 9, and another number leaves remainder 7 when divided by 9. What remainders do their difference (first minus second) and their product leave on division by 9?
Show answer
Difference: 4 − 7 = −3, and −3 + 9 = 6, so the remainder is 6. Product: 4 × 7 = 28 = 9·3 + 1, so the remainder is 1.
Exercise
Find the remainder when 99 × 101 × 103 is divided by 100.
Show answer
99 ≡ −1, 101 ≡ 1, 103 ≡ 3 (mod 100). The product is (−1) × 1 × 3 = −3, and −3 + 100 = 97. The remainder is 97.