Sub topic 4
Euclid's Original Method: Repeated Subtraction
This chapter assumes you know the definition of the greatest common divisor from the first chapter. It also assumes you can read a Python while loop. Euclid described his method in the Elements around 300 BCE. It used no division at all. He used only one move: subtract the smaller number from the larger. Looking at this older version helps explain why the modern remainder-based algorithm works, and why it is faster.
The Key Fact: Subtraction Preserves Common Divisors
Take two positive integers a and b with a > b. Any common divisor of a and b is also a common divisor of a − b and b. The reverse is also true:
- If d divides a and d divides b, then a = dm and b = dn, so a − b = d(m − n). So d divides a − b.
- If d divides a − b and d divides b, then a = (a − b) + b is a sum of two multiples of d. So d divides a.
The pairs (a, b) and (a − b, b) therefore have exactly the same set of common divisors. That means they have the same greatest common divisor:
gcd(a, b) = gcd(a − b, b)
You can apply this again and again. Each step keeps the GCD the same and makes one number smaller. The process stops when the two numbers are equal. At that point gcd(n, n) = n, so either number is the answer.
Tracing an Example
Here is the method on 48 and 18:
| Step | a | b | Action |
|---|---|---|---|
| 0 | 48 | 18 | a > b, so a ← 48 − 18 |
| 1 | 30 | 18 | a > b, so a ← 30 − 18 |
| 2 | 12 | 18 | b > a, so b ← 18 − 12 |
| 3 | 12 | 6 | a > b, so a ← 12 − 6 |
| 4 | 6 | 6 | equal, stop |
The GCD is 6. In steps 0 and 1, 18 was subtracted from 48 twice in a row, leaving 12. That is the same as computing 48 mod 18 = 12. A run of repeated subtractions of the same number is a slow way of doing division with remainder.
Implementation and Step Counting
The function below follows the flowchart. It also counts its steps, so you can compare it with the division-based version introduced in the next chapter.
def gcd_subtraction_steps(a, b):
# Assumes a > 0 and b > 0
steps = 0
while a != b:
if a > b:
a -= b
else:
b -= a
steps += 1
return a, steps
def gcd_division_steps(a, b):
steps = 0
while b != 0:
a, b = b, a % b
steps += 1
return a, steps
print(gcd_subtraction_steps(48, 18)) # (6, 4)
print(gcd_division_steps(48, 18)) # (6, 3)
print(gcd_subtraction_steps(1000, 1)) # (1, 999)
print(gcd_division_steps(1000, 1)) # (1, 1)For balanced inputs such as 48 and 18, the two versions take about the same number of steps. When one input is much larger than the other, the subtraction version slows down badly. For gcd(1000, 1) it subtracts 1 a total of 999 times. In general, the number of steps can grow in proportion to the size of the larger input. The division version reaches the same point in one % operation.
Where Subtraction Still Matters
Subtraction is rarely the best choice on its own, but the idea is still in use. The binary GCD algorithm, covered at the end of this tutorial, combines subtraction with halving. Both are cheap on hardware that has slow division. The subtraction view also gives you an intuition for why the remainder version works: a mod b is what you get after subtracting b from a as many times as possible.
| Property | Subtraction method | Division method |
|---|---|---|
| Basic operation | a − b | a mod b |
| Stopping condition | a == b | b == 0 |
| Valid inputs as written | positive only | any integers (with care) |
| Worst-case steps | proportional to max(a, b) | logarithmic in min(a, b) |
Exercise
Trace the subtraction method on 91 and 35. List each pair, then give the GCD and the number of subtraction steps.
Show answer
(91, 35) → (56, 35) → (21, 35) → (21, 14) → (7, 14) → (7, 7). The GCD is 7, reached after 5 subtraction steps.
Exercise
Which pair (a, b) with 1 ≤ b ≤ a = 100 makes gcdsubtractionsteps take the most steps, and why?
Show answer
The pair (100, 1) takes 99 steps. Each step lowers the larger number by only 1 until both numbers equal 1. No pair with a = 100 can take more steps, because every step lowers a + b by at least 1, and the loop always stops by the time both numbers have fallen to 1.