Sub topic 7
Proving Termination and Correctness
This chapter assumes you already know the identity gcd(a, b) = gcd(b, a mod b) from the earlier chapter. It also assumes you can read the iterative and recursive forms of Euclid's algorithm and are comfortable with proofs by induction. Here we use that identity to prove two separate facts about the algorithm:
- Termination: the loop stops after a finite number of steps for every valid input.
- Partial correctness: if the loop stops, the value it returns is the GCD.
Together these give total correctness. You need both. A loop that never stops never returns a wrong answer, but it is still useless.
The Two Tools: a Variant and an Invariant
A loop variant is a quantity that takes non-negative integer values and strictly decreases on every iteration. Any strictly decreasing sequence of non-negative integers is finite, by the well-ordering principle. So a loop with a variant must stop.
A loop invariant is a statement that is true before the loop starts and stays true after each iteration. When the loop exits, the invariant and the exit condition together should imply the result you want.
For Euclid's algorithm working on non-negative inputs a₀ and b₀:
| Role | Statement |
|---|---|
| Variant | b, the second component of the pair |
| Invariant | gcd(a, b) = gcd(a₀, b₀) |
| Exit condition | b = 0 |
| Conclusion | a = gcd(a, 0) = gcd(a₀, b₀) |
Proof of Termination
Assume b > 0 at the start of an iteration. The division algorithm gives a = qb + r with 0 ≤ r < b. The new second component is r, so it is a non-negative integer strictly smaller than the old b.
The values of b therefore form a strictly decreasing sequence of non-negative integers: b₀ > b₁ > b₂ > … ≥ 0. This sequence has at most b₀ + 1 terms, so b reaches 0 after at most b₀ iterations. That bound is very loose. The next chapter tightens it to a logarithmic one.
Proof of Partial Correctness
Initialization. Before the first iteration, (a, b) = (a₀, b₀), so the invariant holds trivially.
Maintenance. Suppose gcd(a, b) = gcd(a₀, b₀) and b ≠ 0. The new pair is (b, a mod b). By the identity from the earlier chapter, gcd(b, a mod b) = gcd(a, b) = gcd(a₀, b₀).
Exit. The loop exits when b = 0. Every integer divides 0, so the common divisors of a and 0 are exactly the divisors of a. For a ≥ 0 the largest one is a itself, so gcd(a, 0) = a. Combined with the invariant, the returned a equals gcd(a₀, b₀).
The case a₀ = b₀ = 0 also works: the loop never runs and returns 0, which matches the convention gcd(0, 0) = 0.
The Recursive Form, by Strong Induction
For gcd_recursive(a, b), use strong induction on b. In the base case b = 0, the function returns |a| = gcd(a, 0). In the inductive step b > 0, assume the function is correct for every second argument smaller than b. The recursive call has second argument a mod b < b, so by hypothesis it returns gcd(b, a mod b), which equals gcd(a, b). The induction measure is the same quantity as the loop variant.
Checking the Proof in Code
Assertions turn the invariant and variant into runtime checks. They test the claims. They do not prove them.
import math
def gcd_checked(a, b):
a, b = abs(a), abs(b)
target = math.gcd(a, b)
while b != 0:
assert math.gcd(a, b) == target # invariant
r = a % b
assert 0 <= r < b # variant strictly decreases
a, b = b, r
assert a == target # exit condition + invariant
return a
for x, y in [(1071, 462), (48, 18), (0, 5), (7, 0), (-12, 30)]:
print(x, y, gcd_checked(x, y))Common Mistakes in These Proofs
- Proving only the invariant. The invariant also holds for a loop that does nothing, such as
a, b = a, b. Without a variant, you have not shown that the loop stops. - Using a as the variant. On the first iteration, a can increase. For (18, 48), the next pair is (48, 18). The second component is the quantity that always decreases.
- Forgetting the exit step. The invariant alone says nothing about which number is the GCD. You need b = 0 and gcd(a, 0) = a to finish the proof.
Exercise
Euclid's subtraction method replaces (a, b) by (a − b, b) when a > b and by (a, b − a) when b > a, stopping when a = b. For a, b > 0, give a variant that proves it terminates, and explain why it fails when one input is 0.
Show answer
Use a + b. Both values stay positive, and each step subtracts a positive number from one of them, so a + b strictly decreases and is bounded below by 2. If b = 0 and a > 0, the step computes a − 0 = a, so nothing changes, a + b never decreases, and the loop runs forever.
Exercise
The invariant for the iterative algorithm is gcd(a, b) = gcd(a₀, b₀). Would the weaker statement "a and b are both multiples of gcd(a₀, b₀)" be enough to prove correctness?
Show answer
No. It is maintained, because a mod b = a − qb is still a multiple of the GCD. At exit, however, it only tells you that a is some multiple of gcd(a₀, b₀). You also need to know that a divides both a₀ and b₀. The full gcd-equality invariant gives you both facts.