MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
# Problem set 2.1a
# Shemmerson
# Show that it is possible to buy exactly 50, 51, 52, 53, 54, and 55 McNuggets, by finding
# solutions to the Diophantine equation.
a = 6
b = 9
c = 20
for n in range(50, 56):
for d in range(0, n):
for e in range(0, n):
for f in range(0, n):
if a*d + b*e + c*f == n:
print d, 'a', '+', e, 'b', '+', f, 'c', '=', n
# Substitute
# for x in range (56, 66):
# if n + a == x:
# print n, '+', a, '=', x
# if n + b == x:
# print n, '+', b, '=', x
# for
# print d, 'a', '+', e, 'b', '+', f, 'c', '=', n
# To use given solutions to solve range (55, 66)
# Problem set 2.1b
# Shemmerson
# Once the equation is satisfied for x, x +1 ....x +5 then all further answers are
# multiples of the same values.
# Problem set 2.3
# Shemmerson
# Shows largest number of nuggets that cannot be bought in an exact quantity.
nugget=[]
ctr=0
n=0
for n in range (1,50):
test = False
for d in range (0,n):
for e in range (0,n):
for f in range (0,n):
if (6*d) + (9*e) + (20*f) == n:
test = True
if test == True:
ctr +=1
if ctr > 5:
print "Largest exact quantity is",nugget[-1]
else:
nugget.append(n)
ctr = 0
# Problem set 2.4
# Shemmerson
# Same as above with added user control over values
bestSoFar = []
packages = (6,9,20)
ctr = 0
n=0
for n in range(1, 100):
test = False
for a in range(0,n):
for b in range(0,n):
for c in range(0,n):
if (packages[0]*a) + (packages[1]*b) + (packages[2]*c) == n:
test = True
if test == True:
ctr += 1
if ctr == 6:
print bestSoFar[-1], "is the largest quantity not purchasable in 6,9,20 packages"
print "............................................................"
print "Enter your own quantities to find largest quantity not divisable:"
bestSoFar2 = []
ctr1 = 0
n1=0
x = int(raw_input ("First: "))
y = int(raw_input ("Second: "))
z = int(raw_input ("Third: "))
packages = (x,y,z)
for n1 in range(1, 50):
test = False
for a in range(0,n1):
for b in range(0,n1):
for c in range(0,n1):
if (packages[0]*a) + (packages[1]*b) + (packages[2]*c) == n1:
test = True
if test == True:
ctr1 += 1
if ctr1 == 6:
print bestSoFar2[-1], "is the largest exact quantity not purchasable in", + x, ",", y, "and/or", z, "packages"
else:
bestSoFar2.append(n1)
ctr1 = 0
else:
bestSoFar.append(n)
ctr = 0
Shemmerson
4 months ago