In england we have 1, 2, 5, 10, 20, 50 and a pound(100) p coins. Using these coins i would like to work out all the possible combinations that the coins can be added in to make £2.50. The way i approached this question was to make a list of all the possible combinations of all of the coins. To do this i did the following:
ps = [1, 2, 5, 10, 20, 50, 100]
list_of_combos = [[]]
for i in range(7):
for j in range(7):
for k in range(7):
for l in range(7):
for m in range(7):
for n in range(7):
for o in range(7):
print("processing..")
all_combos = (ps[i], ps[j], ps[k], ps[l], ps[m], ps[n], ps[o])
list_of_combos.append(all_combos)
Then from all the possible combos, i tried picking the only ones that actually add up to 250 by doing this.
for i in list_of_combos:
if sum(i) == 250:
print(i)
The problem i am having it that the first nested loop takes forever to complete, which basically makes the program useless. Is there anything i can do to make this loop finish quicker? Thanks.