-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler31.py
More file actions
executable file
·23 lines (19 loc) · 852 Bytes
/
euler31.py
File metadata and controls
executable file
·23 lines (19 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python
# find the number of ways to reach a total with the given number of combinations
pence = 200
denominations = [200, 100, 50, 20, 10, 5, 2, 1]
names = {1: "1p", 2: "2p", 5 : "5p", 10 : "10p", 20 : "20p", 50 : "50p", 100 : "1lb", 200 : "2lb"}
def count_combs(left, i, comb, add):
if add: comb.append(add)
if left == 0 or (i+1) == len(denominations):
if (i+1) == len(denominations) and left > 0:
comb.append( (left, denominations[i]) )
i += 1
while i < len(denominations):
comb.append( (0, denominations[i]) )
i += 1
print " ".join("%d %s" % (n,names[c]) for (n,c) in comb)
return 1
cur = denominations[i]
return sum(count_combs(left-x*cur, i+1, comb[:], (x,cur)) for x in range(0, int(left/cur)+1))
print count_combs(pence, 0, [], None)