Answer
--Algorithm 3 for computing gcd(a, b) when a and b are positive integers with a < b is correct.
Work Step by Step
--We use strong induction on a
-. Basis step:
If a = 0,
-we know that gcd(0, b) = b for all b > 0, and that is precisely
what the if clause does.
--Inductive step:
- Fix k > 0, assume
-the inductive hypothesis—that the algorithm works correctly
-for all values of its first argument less than k—and consider
-what happens with input (k, b), wherek < b. Becausek > 0,
-the else clause is executed, and the answer is whatever the
-algorithm gives as output for inputs (b mod k, k). Because
-b mod k < k, the input pair is valid. By our inductive hypothesis,
-this output is in fact gcd(b mod k, k), which equals
-gcd(k, b) by Lemma 1 in Section 4.3.