Skip to content

Instantly share code, notes, and snippets.

@j0ni
Last active March 3, 2016 04:44
Show Gist options
  • Save j0ni/c79042810345dfa391a9 to your computer and use it in GitHub Desktop.
Save j0ni/c79042810345dfa391a9 to your computer and use it in GitHub Desktop.
Silly bottles problem
;; You've just been hired at Lighthouse Markets, a reputable (and
;; thoroughly fictional) grocery store chain. One of the primary
;; features of Lighthouse Markets is their recycling program on pop
;; bottles. Here is how the program works:
;; * For every two empty bottles, you can get one free (full) bottle of pop
;; * For every four bottle caps, you can get one free (full) bottle of pop
;; * Each bottle of pop costs $2 to purchase
;; Given these parameters, write a program so that you can figure out
;; how many total bottles of pop can be redeemed given a customer
;; investment.
(require-extension numbers)
(define (bottles n)
(define (iter empties caps acc)
(if (and (< empties 2)
(< caps 4))
acc
(let ((new-bottles (+ (quotient empties 2)
(quotient caps 4))))
(iter (+ new-bottles (modulo empties 2))
(+ new-bottles (modulo caps 4))
(+ new-bottles acc)))))
(iter n n n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment