money-exchange calculates combinations of how to change a larger coin into smaller coins

This commit is contained in:
Marco 2024-09-02 16:22:04 +02:00
parent 1b3d763cc6
commit 9a7a74f952
5 changed files with 1376141 additions and 1 deletions

14
money-exchange.scm Normal file
View File

@ -0,0 +1,14 @@
(define (change amount kinds-of-coins)
(cond ((or (< amount 0) (= kinds-of-coins 0)) 0)
((= amount 0) 1)
(else (+ (change amount (- kinds-of-coins 1))
(change (- amount (amount-wo-largest-coin kinds-of-coins)) kinds-of-coins)))))
(define (amount-wo-largest-coin coins-left)
(cond ((= coins-left 5) 50)
((= coins-left 4) 25)
((= coins-left 3) 10)
((= coins-left 2) 5)
((= coins-left 1) 1)))
(print (change 100 5))

View File

@ -19,4 +19,4 @@
(define (sqrt x) (sqrt_iter (/ x 2.0) x x))
(print (sqrt 2))
(print (sqrt 10))

1376126
output Normal file

File diff suppressed because it is too large Load Diff