forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp031.py
35 lines (31 loc) · 1.23 KB
/
p031.py
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
#
# Solution to Project Euler problem 31
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
def compute():
from itertools import combinations_with_replacement
cvs = [1, 2, 5, 10, 20, 50, 100, 200] # Coins and their values
TOTAL = 10 # Use 10 coins (for example)
count = 0 # Solutions (combinations) counter
tracing = True # Not necessary but used just for tracing the combinations
if tracing:
sol = [] # List with solutions
lst = sorted(list(combinations_with_replacement(cvs, TOTAL)))
for i in range(len(lst)):
if sum(lst[i]) == 200:
count += 1
if tracing:
sol.append(lst[i])
print("Total combinations:", count)
if tracing:
print("Solutions:", sol)
'''
Total solutions: 22
Solutions: [(1, 1, 1, 1, 1, 5, 20, 20, 50, 100), (1, 1, 1, 2, 5, 10, 10, 20, 50, 100), (1, 1, 1, 2, 5, 20, 20, 50, 50, 50), (1, 1, 2, 2, 2, 2, 20, 20, 50, 100), ...
(10, 10, 10, 20, 20, 20, 20, 20, 20, 50), (20, 20, 20, 20, 20, 20, 20, 20, 20, 20)]
'''
if __name__ == "__main__":
compute()