; Function to compute the greatest common divisor of two non-negative integers. ; Based on Euclidean algorithm. Recursive Scheme version. (define gcd (lambda (a b) (if (= b 0) a (gcd b (modulo a b))))) ; Iterative Scheme version. (define gcd (lambda (a b) (do ((c 0)) ; local variable c initialized to 0 ((= b 0) a) ; loop until b is 0; then return a (set! c (modulo a b)) ; three expressions in body of loop (set! a b) (set! b c)))) # Yuck. That Scheme loop is unholy. Recursive Python version. def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) # Iterative Python version. def gcd(a, b): while (b != 0): c = a % b a = b b = c return a