Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplified solution - really needs memoization to get good performanc…

…e with high numbers though
  • Loading branch information...
commit b3bfc400c98de536fee9180ccf0833d3f46137b4 1 parent e32eff8
@peterjoel authored
Showing with 5 additions and 6 deletions.
  1. +5 −6 src/Problem0031.hs
View
11 src/Problem0031.hs
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.