From 1d15785f23c0c44ce469ec9817fae9fd2eaab4ed Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 27 Nov 2011 21:55:32 +0900 Subject: [PATCH] making gcd and lcm n-ary --- lib/init-7.scm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/init-7.scm b/lib/init-7.scm index f95332ad..a25245d5 100644 --- a/lib/init-7.scm +++ b/lib/init-7.scm @@ -978,14 +978,26 @@ (if (<= res 0) res (+ res b)) (if (>= res 0) res (+ res b))))) -(define (gcd a b) +(define (gcd2 a b) (if (= b 0) (abs a) (gcd b (remainder a b)))) -(define (lcm a b) +(define (gcd . args) + (if (null? args) + 0 + (let lp ((x (car args)) (ls (cdr args))) + (if (null? ls) x (lp (gcd2 x (car ls)) (cdr ls)))))) + +(define (lcm2 a b) (abs (quotient (* a b) (gcd a b)))) +(define (lcm . args) + (if (null? args) + 1 + (let lp ((x (car args)) (ls (cdr args))) + (if (null? ls) x (lp (lcm2 x (car ls)) (cdr ls)))))) + (define (max x . rest) (let lp ((hi x) (ls rest)) (if (null? ls)