Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define coins (vector-immutable 50 25 10 5 1))
- (define coins-amount (vector-length coins))
- (define cache-width 1000001)
- (define cache (make-vector (* cache-width (add1 coins-amount)) #f))
- (define (count-exchanges sum coin-index)
- (let* ([index (+ sum (* coin-index cache-width))]
- [entry (vector-ref cache index)]
- [result
- (cond [entry entry]
- [(zero? sum) 1]
- [(negative? sum) 0]
- [(>= coin-index coins-amount) 0]
- [else
- (begin
- (let find-smaller-coin ()
- (when (and (< coin-index coins-amount)
- (< sum (vector-ref coins coin-index)))
- (set! coin-index (add1 coin-index))
- (find-smaller-coin)))
- (+ (count-exchanges (- sum (vector-ref coins coin-index)) coin-index)
- (count-exchanges sum (add1 coin-index))))])])
- (unless entry
- (vector-set! cache index result))
- result))
- (time (count-exchanges 1000000 0))
Add Comment
Please, Sign In to add comment