mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 00:31:21 +00:00
On my way to problem 31!
Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
55
e_31.py
55
e_31.py
@@ -12,27 +12,40 @@ It is possible to make L2 in the following way:
|
||||
1 * L1 + 1 * 50p + 2 * 20p + 1 * 5p + 1 * 2p + 3 * 1p
|
||||
How many different ways can L2 be made using any number of coins?
|
||||
'''
|
||||
def combinations_with_replacement(iterable, r):
|
||||
# combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
if not n and r:
|
||||
return
|
||||
indices = [0] * r
|
||||
yield tuple(pool[i] for i in indices)
|
||||
while True:
|
||||
for i in reversed(range(r)):
|
||||
if indices[i] != n - 1:
|
||||
break
|
||||
else:
|
||||
return
|
||||
indices[i:] = [indices[i] + 1] * (r - i)
|
||||
yield tuple(pool[i] for i in indices)
|
||||
|
||||
def listmul(list1, list2):
|
||||
"""Multiplies each elements of list1 and list 2 with each other"""
|
||||
if len(list1) != len(list2):
|
||||
raise ValueError("Lists are supposed to have the same length!")
|
||||
|
||||
else:
|
||||
out = []
|
||||
for ii in range(len(list1)):
|
||||
out.append(list1[ii] * list2[ii])
|
||||
|
||||
return out
|
||||
|
||||
if __name__ == '__main__':
|
||||
aa = combinations_with_replacement(range(8), 8)
|
||||
val = 0
|
||||
for a in aa:
|
||||
print sum(a)
|
||||
print val
|
||||
|
||||
values = [1, 2, 5, 10, 20, 50, 100, 200]
|
||||
goal = 200
|
||||
|
||||
max_vals = [goal / val for val in values]
|
||||
# we have to solve the equation a * 1 + b * 2 + c * 5 + d * 10 + e * 20 + f * 50 + g *100 + h * 200 = goal
|
||||
cur_vals = [0] * len(values)
|
||||
|
||||
sols = 0
|
||||
for cur_vals[0] in range(max_vals[0] + 1):
|
||||
print cur_vals[0]
|
||||
for cur_vals[1] in range(max_vals[1] + 1):
|
||||
print cur_vals[1]
|
||||
for cur_vals[2] in range(max_vals[2] + 1):
|
||||
for cur_vals[3] in range(max_vals[3] + 1):
|
||||
for cur_vals[4] in range(max_vals[4] + 1):
|
||||
for cur_vals[5] in range(max_vals[5] + 1):
|
||||
for cur_vals[6] in range(max_vals[6] + 1):
|
||||
for cur_vals[7] in range(max_vals[7] + 1):
|
||||
if sum(listmul(cur_vals, values)) == goal:
|
||||
sols += 1
|
||||
print sols
|
||||
|
||||
Reference in New Issue
Block a user