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
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 by
Codex 0 2026-10-31
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 aThis 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!