mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-20 06:09:18 +02:00
making gcd and lcm n-ary
This commit is contained in:
parent
7b7c2aed8e
commit
1d15785f23
1 changed files with 14 additions and 2 deletions
|
@ -978,14 +978,26 @@
|
||||||
(if (<= res 0) res (+ res b))
|
(if (<= res 0) res (+ res b))
|
||||||
(if (>= res 0) res (+ res b)))))
|
(if (>= res 0) res (+ res b)))))
|
||||||
|
|
||||||
(define (gcd a b)
|
(define (gcd2 a b)
|
||||||
(if (= b 0)
|
(if (= b 0)
|
||||||
(abs a)
|
(abs a)
|
||||||
(gcd b (remainder a b))))
|
(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))))
|
(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)
|
(define (max x . rest)
|
||||||
(let lp ((hi x) (ls rest))
|
(let lp ((hi x) (ls rest))
|
||||||
(if (null? ls)
|
(if (null? ls)
|
||||||
|
|
Loading…
Add table
Reference in a new issue