looping through possible combinations of McNuggets packs of 6, 9 and 20
Baba
raoulbia at gmail.com
Sun Aug 15 19:11:48 CEST 2010
Hi All,
@Emile tnx for spotting the mistake. Should have seen it myself.
@John & Ian i had a look around but couldn't find a general version of
below theorem
If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some x,
then it is possible to buy any number of McNuggets >= x, given that
McNuggets come in 6, 9 and 20 packs.
I must admit that i'm not keen on spending too much time on purely
mathematical questions. It seems that this has to do with Frobenius
Number? re range the exercise seems to suggest to limit it to 200
so feel free to enlighten me on this last part. In the meantime the
almost complete code is:
def can_buy(n_nuggets,packages):
for a in range (0,n_nuggets/packages[0]+1):
for b in range (0,n_nuggets/packages[1]+1):
for c in range (0,n_nuggets/packages[2]+1):
#print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
if packages[0]*a+packages[1]*b
+packages[2]*c==n_nuggets:
return True
return False
def diophantine_nuggets(*packages):
cbc=0 #cbc=can_buy counter
#packages =[x,y,z]
for n_nuggets in range(200):
result=can_buy(n_nuggets,packages)
if result:
cbc+=1
else:
cbc=0
if cbc==6:
solution=n_nuggets-6
print "Largest number of McNuggets that cannot be bought in
exact quantity: %d" %(solution)
break
diophantine_nuggets(2,3,4)
More information about the Python-list
mailing list