Advertisement
Indivicivet

Project Euler Problem 31, solution by Indi

Jul 16th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # Project Euler Problem 31, solution by Indi
  2.  
  3. TOTAL = 200 # can't go above this, but can go below (and pad with 1s)
  4. DENOMS = [200, 100, 50, 20, 10, 5, 2] # amount of 1s fixed once we allocate the rest
  5.  
  6. dot = lambda a,b:sum([a[i]*b[i] for i in range(len(a))]) # dot product (truncates second param)
  7.  
  8. prevConfig = [[]]   # each config is a list of amounts of denominations
  9. config = []         # we're basically making a tree here
  10. for d in DENOMS:
  11.     for c in prevConfig:
  12.         for q in range((TOTAL - dot(c, DENOMS))//d + 1):
  13.             config.append(c+[q,]) # add each possible thing; could use list comprehension
  14.     prevConfig = list(config) # make sure we copy!
  15.     config = [] # reset config for next "depth"
  16.  
  17. print(len(prevConfig)) #73682
  18. # Rachel has a far cleverer solution than this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement