5.16 · exercise

gcd, the euclid way

Euclid noticed around 300 BC that the greatest common divisor of two numbers does not change when you replace the larger with its remainder by the smaller. Keep doing that and one of them hits zero; the other is the answer.

The starter loads Euclid's own example pair, 1071 and 462. Write the loop: remainder via udiv plus msub, shuffle, repeat while the second value is nonzero.

what is checked

  • the gcd is printed on its own line
  • the program exits cleanly
  • the remainder loop does the work (udiv, msub, and no smuggling the answer in)

Specification

args
stdoutprints the expected output
exitexits with the expected code
sourceuses udiv
sourceuses msub
sourcecomputes the result (does not hardcode it)

Checked by running your program against expected behavior, never by matching a stored solution.

loading editor...
Open in playground