[This page is auto-generated; please prefer to read the article’s PDF Form.]
We will prove the correctness of Euclid’s Algorithm by arguing about the above program. We first claim that, the invariants of its loop are:
The loop’s condition is:
|
So, whenever an iteration is entered (condition C true) with P being true, we must have:
|
The assignment (2) (or (3)) in the iteration will update a (or b) to a mod b (or b mod a), leaving it non-negative. Thus, P will continue to hold after the iteration, and so is a loop-invariant.
Additionally, the change made to a or b by an iteration does not change gcd(a,b) due to theorem 2. Thus, every iteration, though modifies a or b, keeps gcd(a,b) intact. Which means, after any number of iterations, gcd(a,b) would retain its initial value, which is gcd(A,B) = G. Thus, after each iteration Q must hold, making it a loop-invariant.
Now we will prove that the loop must terminate. Consider the function t(a,b) = a + b. Before the loop starts, t(a,b) = A + B. Since the loop maintains invariant P, after each iteration t(a,b) ≥ 0. Every iteration also strictly decreases a or b, and hence t(a,b). So, t(a,b) attains only non-negative integer values, starting at A + B and strictly decreasing after each iteration. As there are only finite integers between 0 and A + B, the iterations cannot go on indefinitely and must terminate.
What will have been achieved when the loop terminates? At that point, condition C must not hold, but P and Q must hold (as they do after each iteration). So, we must have:
|
Thus, a must contain the value of gcd(A,B). This completes the proof.
Copyright © 2021 Nitin Verma. All rights reserved.