[Main website]

dok/example/common-greather-denominator

fun (x::Int, y::Int).gcd::Int {
   :# Greater common divisor

   :require x > 0 && y > 0

   repeat {
     :max-repetitions [x, y]*.max

     :assert x > 0 && test y > 0

     when x !== y

     set x case {
       when x > y
       return x - y
     } -else {
       when x < y
       return y - x
     }

     :assert-informal [
       the new pair ``(x, y)`` has the same greatest common divisor as the old pair ``(a, b)``
     ]
   }

   :assert x == y

   return x
}

Links to this note