Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Variable definitions:
- # a = # of 6 McNugget packages
- # b = # of 9 McNugget packages
- # c = # of 20 McNugget packages
- # n = test # of McNuggets to be purchased
- # successful_n = a list of all the "n" values that can be purchased
- # bestSoFar = the highest number that cannot be purchased
- #Initialize variables
- successful_n = [0]
- bestSoFar = 0
- n = 1
- a = 0
- #Start loops to test all possible combinations of a, b and c
- #Store successful 'n's in successful_n and failed numbers in bestSoFar
- while successful_n[-1] - bestSoFar < 6:
- while a <= n/6:
- b = 0
- while b <= n/9:
- c = 0
- while c <= n/20:
- result = 6*a + 9*b + 20*c
- if result == n:
- if successful_n.count(n) == 0:
- successful_n.append(n)
- c = c + 1
- b = b + 1
- a = a + 1
- if successful_n.count(n) == 0:
- bestSoFar = n
- n = n + 1
- a = 0
- ##print successful_n #used in testing phase
- ##print bestSoFar #used in testing phase
- print """
- Given package sizes of 6, 9 and 20, the largest quantity of McNuggets
- that cannot be purchased in an exact quantity is:
- """
- print bestSoFar
Advertisement
Add Comment
Please, Sign In to add comment