-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path031-coin-sums.lhs
executable file
·46 lines (29 loc) · 963 Bytes
/
031-coin-sums.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env runhaskell
[Coin sums](http://projecteuler.net/problem=31)
-----------------------------------------------
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).
It is possible to make £2 in the following way:
1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p
How many different ways can £2 be made using any number of coins?
Code
----
> coins :: [Int]
> coins = [1, 2, 5, 10, 20, 50, 100, 200]
> numParts :: Int -> Int -> Integer
> numParts 0 _ = 1
> numParts n s -- s = smallest coin to use
> | n < s = 0
> | otherwise =
> let cs = dropWhile (< s) coins
> in sum $ map (\ c -> numParts (n-c) c) cs
> main :: IO ()
> main = let result = numParts 200 0
> in print result
Answer
------
73682
Performance
-----------
8.03s user 0.06s system 100% cpu 8.082 total