euler/Problem031.hs

20 lines
715 B
Haskell

-- In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
--
-- 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p)
--
-- How many different ways can £2 be made using any number of coins?
import Data.Array
import Euler
coins = listArray (0,7) [1,2,5,10,20,50,100,200] :: Array Int Int
nWays total = arr ! (total, snd (bounds coins))
where
arr = array ((0, fst (bounds coins)), (total, snd (bounds coins)))
[ ((n, c), sum [ arr ! (n - (coins!c'), c') + if n == coins!c' then 1 else 0
| c' <- [fst (bounds coins)..c], coins!c' <= n ])
| n <- [0..total], c <- indices coins ]
main = print $ nWays 200