// file: gcd.rex // author: R. Keller // purpose: greatest-common divisor // (Euclid's algorithm) gcd(x, 0) => x; gcd(0, y) => y; gcd(x, y) => x <= y ? gcd(y%x, x) : gcd(x%y, y); // Rationale: If one number is 0, the gcd is the other number // Otherwise neither is 0, so replacing the larger number by it divided // by the smaller gives a pair of numbers with the same gcd (This needs // to be proved. test() = [gcd(36, 28), gcd(0, 99), gcd(99, 0), gcd(1234567, 765438)];