Skip to content

Commit

Permalink
simplified solution - really needs memoization to get good performanc…
Browse files Browse the repository at this point in the history
…e with high numbers though
  • Loading branch information
petejoel committed Mar 10, 2012
1 parent e32eff8 commit b3bfc40
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Problem0031.hs
Expand Up @@ -25,13 +25,12 @@ module Problem0031 (
type Coin = Int

run :: IO Int
run = return $ length (ways 200 coins)
run = return $ ways 200 coins


coins = [200,100,50,20,10,5,2,1]

ways :: Int -> [Coin] -> [[(Coin,Int)]]
ways amount [c] = [[(c, amount `div` c)]]
ways amount (c:coins) = concat $ map drill [0 .. amount `div` c]
where drill n = map ((c,n):) (ways (amount-c*n) coins)

ways :: Int -> [Coin] -> Int
ways amount [c] = 1
ways amount (c:coins) = sum $ map count [0 .. amount `div` c]
where count n = ways (amount - c * n) coins

0 comments on commit b3bfc40

Please sign in to comment.