Maximum common divisor algorithm