Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/discrete-mathematics-continuous-mathematics-and-probability/2/b/solution

For integers not both zero, replace them first by their absolute values, then repeat integer division:
while b != 0:
    (a,b) := (b, a mod b)
return a
This is the Euclidean algorithm. Its loop invariant is that equals the original greatest common divisor. When , the new remainder lies in , so is a strictly decreasing nonnegative loop variant. At termination, , proving correctness, including the case where one original input is zero.

New to topics? Read the docs here!