Skip to content

Instantly share code, notes, and snippets.

@alcol80
Last active August 29, 2015 14:22
Show Gist options
  • Save alcol80/2c3313f5be436d0ea609 to your computer and use it in GitHub Desktop.
Save alcol80/2c3313f5be436d0ea609 to your computer and use it in GitHub Desktop.
-- Given an infinite number of quarters (25 cents), dimes (10 cents),
-- nickels (5 cents) and pennies (1 cent), write code to calculate the
-- number of ways of representing n cents.
module Coins where
comb cents = [(q, d, n, p) | q <- rng 25 cents,
d <- rng 10 (cents - 25*q),
n <- rng 5 (cents - 25*q - 10*d),
p <- rng 1 (cents - 25*q - 10*d - 5*n),
25*q + 10*d + 5*n + p == cents]
where rng k max = [0..(max `div` k)]
result = length . comb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment