SketchyLISP Reference |
Copyright (C) 2006 Nils M Holm |
<<[expt] | [Index] | [id]>> |
Conformance: R5RS
Purpose: Compute the greatest common divisor (GCD) of two integer numbers. This function uses Euclid's Algorithm.
Arguments:
A - number
B - number
Implementation:
(define (gcd . a) (letrec ((_gcd (lambda (a b) (cond ((zero? b) a) ((zero? a) b) ((n< a b) (_gcd a (nremainder b a))) (#t (_gcd b (nremainder a b))))))) (reduce _gcd (map (lambda (x) (natural (abs x))) a) 0)))
Example:
(gcd 99 -77) => 11
<<[expt] | [Index] | [id]>> |