Sub topic 2
Division with Remainder
This chapter assumes you can do basic integer arithmetic, read short Python functions, and follow a short proof. Euclid's algorithm repeats one operation, division with remainder, so it is worth knowing exactly what that operation guarantees and where programming languages differ in how they compute it.
The Division Theorem
For any integer a and any positive integer b, there is exactly one pair of integers q (the quotient) and r (the remainder) such that
a = b * q + r, and0 <= r < b.
Here a is the dividend and b is the divisor. The second condition matters most for Euclid's algorithm. The remainder is always strictly smaller than the divisor, so every step of the algorithm produces a smaller number than the one before it.
For example, dividing 1071 by 462 gives 1071 = 462 * 2 + 147. The remainder 147 satisfies 0 <= 147 < 462.
Why q and r exist
Look at the numbers a - b*q for every integer q, and keep only the ones that are not negative. That set is never empty, because a very negative q makes a - b*q large. By the well-ordering principle, the set has a smallest element. Call it r. If r >= b, then r - b would also be in the set and would be smaller than r, which is a contradiction. So 0 <= r < b.
Why they are unique
Suppose a = b*q + r = b*q' + r' and both remainders lie in [0, b). Then b*(q - q') = r' - r. The right side is strictly between -b and b, and the left side is a multiple of b. The only multiple of b in that range is 0, so q = q' and r = r'.
Uniqueness is why the expression a mod b has one definite value. That value is the r from the theorem.
A Picture of the Operation
For a non-negative a, you can think of division with remainder as subtracting b until you can't subtract it again.
Real hardware and Python's % operator compute the result directly instead of looping. The loop is only a way to see why r ends up below b.
Division with Remainder in Python
Python's // and % use floor division: q is a / b rounded down. When b > 0, this gives exactly the q and r from the theorem, including for negative a. The divmod function returns both values at once.
def division_with_remainder(a, b):
if b <= 0:
raise ValueError("divisor must be positive")
q, r = divmod(a, b)
assert a == b * q + r and 0 <= r < b
return q, r
for a in (17, -17, 1071, 0):
q, r = division_with_remainder(a, 5)
print(f"{a} = 5 * {q} + {r}")Output:
17 = 5 * 3 + 2
-17 = 5 * -4 + 3
1071 = 5 * 214 + 1
0 = 5 * 0 + 0The case -17 is the one to remember. The theorem requires a non-negative remainder, so q is -4, not -3, and r is 3.
Common Errors: Languages Disagree on Signs
Many languages truncate toward zero instead of rounding down. In those languages, the remainder takes the sign of the dividend.
| Expression | Python 3 | C, C++, Java, JavaScript |
|---|---|---|
| `17 % 5` | 2 | 2 |
| `-17 % 5` | 3 | -2 |
| `17 % -5` | -3 | 2 |
| `-17 % -5` | -2 | -2 |
Two more mistakes come up often:
- Dividing by zero.
a % 0raisesZeroDivisionErrorin Python. Euclid's algorithm has to stop when the divisor reaches 0. It must not try to divide by it. - Using float division.
a / breturns a float. Large integers lose precision as floats, soint(a / b)can return the wrong quotient. Use//,%, ordivmodfor integer work.
Why This Matters for the GCD
Euclid's algorithm relies on two facts from this chapter. First, r = a - b*q, which means r is built from a and b using only multiplication and subtraction. The next chapter uses this to show that a, b, and r share the same common divisors. Second, 0 <= r < b, so the numbers get smaller at every step and the algorithm must stop.
Exercise
Find the quotient and remainder that the division theorem gives for (a) 1071 divided by 462, (b) -7 divided by 3, and (c) 462 divided by 147. Check each answer against a = b*q + r with 0 <= r < b.
Show answer
(a) q = 2, r = 147, since 4622 + 147 = 1071. (b) q = -3, r = 2, since 3(-3) + 2 = -7. A truncating language would give q = -2 and r = -1, which breaks the condition r >= 0. (c) q = 3, r = 21, since 147*3 + 21 = 462.
Exercise
In Python, what does divmod(-20, 6) return? What would a C program print for -20 / 6 and -20 % 6?
Show answer
Python returns (-4, 4), because 6(-4) + 4 = -20. C truncates toward zero and prints -3 and -2, because 6(-3) + (-2) = -20.