Sub topic 4
Euclid's Original Method: Repeated Subtraction
The algorithm most programmers know as "Euclid's algorithm" uses the remainder operator. Euclid himself did not describe it that way. In Book VII of the Elements (Propositions 1 and 2), he finds the greatest common measure of two numbers by repeatedly taking the smaller one away from the larger. This chapter looks at that original subtraction-based form: why it works, how to implement it, and why it can be very slow. Knowing where it is slow explains why the division-based version replaced it.
This chapter assumes you know the definitions of divisor, common divisor, and greatest common divisor from the first chapter. It also assumes you can read a Python while loop. You do not need the remainder invariant from the previous chapter, but this chapter will connect back to it.
The Core Idea: Subtracting Preserves Common Divisors
The subtraction method rests on one identity. For positive integers a > b:
gcd(a, b) = gcd(a − b, b)
The proof shows that the pairs (a, b) and (a − b, b) have exactly the same set of common divisors. If two sets are equal, their largest elements are equal too.
- Suppose d divides both a and b. Then a = d·m and b = d·n for some integers m and n, so a − b = d·(m − n). So d divides a − b, and d is a common divisor of (a − b, b).
- Suppose d divides both a − b and b. Then a = (a − b) + b is a sum of two multiples of d, so d divides a. So d is a common divisor of (a, b).
Each direction takes one line of algebra. The subtraction identity needs no division, no remainders, and no quotients. That is one reason Euclid could state it in purely geometric language. He thought of numbers as lengths, and his question was which length "measures" both.
The Algorithm
Apply the identity over and over. Each step replaces the larger number with the difference. When the two numbers become equal, stop. gcd(x, x) = x, so the shared value is the answer.
Here it is in Python:
def gcd_subtraction(a, b):
# Assumes a > 0 and b > 0
while a != b:
if a > b:
a -= b
else:
b -= a
return a
print(gcd_subtraction(48, 18)) # 6Tracing 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, return 6 |
The method took four subtractions. Every pair in the table has the same common divisors {1, 2, 3, 6}, and the largest of these is 6.
Why It Stops
While both values are positive and unequal, each step replaces the larger value with a smaller positive value. The difference of two unequal positive integers is positive, so the larger one shrinks and nothing reaches zero. As a result, max(a, b) strictly decreases at every step and stays a positive integer. A strictly decreasing sequence of positive integers cannot go on forever, so the loop must reach a = b. A later chapter proves termination formally for the division-based version, and that argument has the same shape.
Relating Subtraction to Division
Watch the 48, 18 trace in runs. The first two steps both subtract 18 from a. That run takes 48 down to 12, and 12 is exactly 48 mod 18. The quotient 48 // 18 = 2 counts how many times 18 was subtracted.
In other words, a single run of repeated subtraction of the same value is a division step done one unit at a time. The table below groups the trace by runs:
| Run | Pair before | Subtracted | Times | Pair after | Same as |
|---|---|---|---|---|---|
| 1 | (48, 18) | 18 | 2 | (12, 18) | 48 = 2·18 + 12 |
| 2 | (12, 18) | 12 | 1 | (12, 6) | 18 = 1·12 + 6 |
| 3 | (12, 6) | 6 | 1 | (6, 6) | 12 = 2·6 + 0, stopped one early |
The last run stops when the values become equal instead of when one becomes zero. So it uses one subtraction fewer than its quotient. This gives an exact count:
number of subtractions = (sum of the quotients in the division-based algorithm) − 1
For 48 and 18, the quotients are 2, 1, 2. Their sum is 5, and 5 − 1 = 4, which matches the trace.
You can check the formula with an instrumented version:
def gcd_subtraction_steps(a, b):
# Assumes a > 0 and b > 0; returns (gcd, number of subtractions)
steps = 0
while a != b:
if a > b:
a -= b
else:
b -= a
steps += 1
return a, steps
def quotient_sum(a, b):
# Sum of quotients in the division-based algorithm, for a, b > 0
total = 0
while b != 0:
total += a // b
a, b = b, a % b
return total
for a, b in [(48, 18), (1071, 462), (21, 13), (100, 1), (1000, 999)]:
g, steps = gcd_subtraction_steps(a, b)
print(a, b, "gcd =", g, "subtractions =", steps,
"quotient sum - 1 =", quotient_sum(a, b) - 1)Running it prints:
48 18 gcd = 6 subtractions = 4 quotient sum - 1 = 4
1071 462 gcd = 21 subtractions = 11 quotient sum - 1 = 11
21 13 gcd = 1 subtractions = 6 quotient sum - 1 = 6
100 1 gcd = 1 subtractions = 99 quotient sum - 1 = 99
1000 999 gcd = 1 subtractions = 999 quotient sum - 1 = 999For 1071 and 462, the division quotients are 2, 3, 7 (remainders 147, 21, 0). The quotients sum to 12, and the subtraction method takes 11 steps where the division method takes 3.
Where Subtraction Is Slow
The step count depends on the quotients, not on the number of division steps. A single large quotient costs one division but many subtractions.
The worst case is easy to build. Take b = 1 and a large a. The division method finishes in one step, because a mod 1 = 0. The subtraction method subtracts 1 from a until a reaches 1, which takes a − 1 steps. For gcd(10**9, 1), that is 999,999,999 loop iterations. Even pairs that look harmless can be slow. In (1000, 999), the first subtraction gives (1, 999), and then 998 more subtractions follow.
The step count can therefore grow linearly in the size of the inputs. Measured against the number of digits n, that is exponential: about 10^n steps for an n-digit input. The division-based algorithm needs a number of steps that grows only linearly in n, as the chapter on Lamé's theorem shows.
| Input pair | Division steps | Subtractions |
|---|---|---|
| (48, 18) | 3 | 4 |
| (1071, 462) | 3 | 11 |
| (21, 13) | 6 | 6 |
| (1000, 999) | 2 | 999 |
| (10**9, 1) | 1 | 999,999,999 |
Look at (21, 13), a pair of consecutive Fibonacci numbers. Here the two methods do almost identical work, because every quotient except the last is 1. When every quotient is 1, a division step and a subtraction step do the same thing. That makes Fibonacci pairs the worst case for the division method, but not for the subtraction method. The subtraction method is worst when quotients are large, and the division method is worst when quotients are all small.
Inputs the Simple Version Cannot Handle
The function above is correct only when both inputs are positive. Other inputs break it in ways that are hard to spot:
- One input is zero. With
gcd_subtraction(0, 5), the loop sees 0 < 5 and computes b = 5 − 0 = 5 forever. The program hangs instead of returning gcd(0, 5) = 5. - Both inputs are zero.
gcd_subtraction(0, 0)returns 0 immediately. That happens to match the usual convention, but only by accident. - Negative inputs. With
gcd_subtraction(-4, 6), the loop computes b = 6 − (−4) = 10, then 14, and so on. The values grow without bound, and the loop never ends.
The termination argument assumed both values were positive, and these failures are exactly where that assumption breaks. A defensive wrapper takes absolute values and handles zero before entering the loop:
def gcd_subtraction_safe(a, b):
a, b = abs(a), abs(b)
if a == 0:
return b
if b == 0:
return a
while a != b:
if a > b:
a -= b
else:
b -= a
return a
print(gcd_subtraction_safe(0, 5)) # 5
print(gcd_subtraction_safe(-4, 6)) # 2
print(gcd_subtraction_safe(48, -18)) # 6The chapter on edge cases explains why these conventions are the right ones and applies them to the division-based algorithm.
The Geometric Picture
Euclid's version also has a geometric reading. Take a 48 × 18 rectangle. Cut off as many 18 × 18 squares as fit: two of them, which leaves a 12 × 18 rectangle. From that, cut a 12 × 12 square, leaving 12 × 6. Cut one 6 × 6 square, and a 6 × 6 square remains. Since the last piece is a square, its side, 6, is the largest square tile that covers the original rectangle exactly. Each cut is one subtraction, and each group of equal cuts is one division step. The Greeks called this process anthyphairesis, meaning "reciprocal subtraction." In Book X of the Elements, they ran the same process on lengths that might never become equal. A process that never ends is how they recognized that two lengths have no common measure, such as the side and diagonal of a square.
Summary
- Subtracting the smaller number from the larger preserves the set of common divisors, so gcd(a, b) = gcd(a − b, b).
- Repeating the subtraction until the two values are equal gives the GCD. Termination follows from the larger value strictly decreasing while staying positive.
- A run of equal subtractions is one division step done slowly. The number of subtractions equals the sum of the division quotients minus one.
- Large quotients make the method slow, up to a − 1 steps for gcd(a, 1). That cost is why the remainder-based version replaced it.
- The plain version hangs on zero or negative inputs unless you normalize the inputs first.
Exercise
Without running code, how many subtractions does gcd_subtraction(1071, 462) perform? Use the division quotients to compute it, then list the pair after each complete run.
Show answer
The division steps are 1071 = 2·462 + 147, 462 = 3·147 + 21, and 147 = 7·21 + 0, so the quotients are 2, 3, 7. Their sum is 12, so there are 12 − 1 = 11 subtractions. After the first run (2 subtractions) the pair is (147, 462). After the second run (3 subtractions) it is (147, 21). The last run subtracts 21 six times to reach (21, 21) and returns 21.
Exercise
Find a pair of positive integers, each at most 100, where the subtraction method takes exactly the same number of steps as the division method takes division steps. Explain why, in general, the two counts can only match when every quotient except the last is 1 and the last quotient is 2.
Show answer
(21, 13) works: both methods take 6 steps, and the quotients are 1, 1, 1, 1, 1, 2. Any pair of consecutive Fibonacci numbers greater than 2 behaves the same way, for example (8, 5) or (89, 55). In general, if the division method has k steps with quotients q1 through qk, the subtraction count is q1 + ... + qk − 1. Every quotient is at least 1, and the last quotient is at least 2 because the final remainder is 0 and the divisor is smaller than the dividend. So the sum is at least (k − 1) + 2 = k + 1, and the subtraction count is at least k. Equality holds exactly when the first k − 1 quotients are 1 and the last is 2.
Exercise
The call gcd_subtraction(12, 0) never returns. Trace the first three iterations, then explain which assumption of the termination argument fails.
Show answer
With a = 12 and b = 0, a > b, so a becomes 12 − 0 = 12. The pair stays (12, 0) on every iteration, and a == b is never true. The termination argument needs the larger value to strictly decrease. Subtracting 0 leaves it unchanged, so the argument fails. Normalizing the inputs (if b == 0, return a) fixes this.