Loading [MathJax]/extensions/tex2jax.js

2010年7月20日火曜日

Determine the greatest common divisor of two positive integer numbers

;; P32 (**) Determine the greatest common divisor of two positive integer numbers.
;; Use Euclid's algorithm.
;; Example:
;; * (gcd 36 63)
;; 9
(define (gcd a b)
(let loop ((x a) (y b) (z 1) (w 0))
(let ((q (quotient x y)) (r (modulo x y)))
(if (zero? r)
y
(loop y r w (- z (* q w)))))))
view raw p32.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿