About Me
No description provided.
Classes
|
Class status: Established
Role:
Student
|
.
17% complete
|
Submitted Assignments
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
###PS2a###
n = 1 #The desired total number of nuggets, starting with 1
maxTest = 20 #the largest polynomial coefficient to test
cantObtain = 0 #last found number can't be obtained by combo of 6, 9, 20
consecutive = 0 #number of consecutive combinations found
found = False #have we found a valid combination?
while consecutive < 6:
found = False
#these for loops try every combination of coefficients up to maxTest
for a in range(maxTest):
for b in range(maxTest):
for c in range(maxTest):
#print 'a:{0:2d} b:{1:2d} c:{2:2d} -> {3:4d}'.format(a, b, c, 6*a+9*b+20*c)
if 6*a+9*b+20*c == n:
print 'Found a combination for', n
found = True
if found == False:
cantObtain = n
consecutive = 0
elif found == True:
consecutive += 1
n += 1
print 'The last unobtainable number was', cantObtain
###PS2b###
n = 1 #The desired total number of nuggets, starting with 1
maxTest = 40 #the largest polynomial coefficient to test
cantObtain = 0 #last found number can't be obtained by combo of tuple box
consecutive = 0 #number of consecutive combinations found
found = False #have we found a valid combination?
#box = (1, 2, 3)
#box = (6, 9, 20)
#box = (3, 10, 12)
#box = (4, 6, 9)
#box = (2, 4, 6)
box = (7, 9, 11)
while consecutive < 6:
found = False
#these for loops try every combination of coefficients up to maxTest
for a in range(maxTest):
for b in range(maxTest):
for c in range(maxTest):
#print 'a:{0:2d} b:{1:2d} c:{2:2d} -> {3:4d}'.format(a, b, c, 6*a+9*b+20*c)
if box[0]*a+box[1]*b+box[2]*c == n:
print 'Found a combination for', n
found = True
if found == False:
cantObtain = n
consecutive = 0
elif found == True:
consecutive += 1
if n >= 200:
print 'Limit exceeded, probably no answer'
break
n += 1
print "Given package sizes " + str(box[0]) + ", " + str(box[1]) + " and " + str(box[2]) + " the largest number of McNuggets that cannot be bought in exact quantity is: " + str(cantObtain)
Nicholas123
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
###PS1a###
#print 2, because it is exceptional in being the only even (non-odd) prime
print '{0:4d} {1:4d}'.format(1, 2)
numPrimesCalcd = 1 #because we already printed '2'
candidate = 3 #the first number to test
divTest = 3 #the minimum number a given prime might divide by
while numPrimesCalcd < 1000 :
while divTest <= candidate :
if divTest == candidate :
# It's a prime, because it hasn't divided by anything until it divided by itself
numPrimesCalcd += 1
print '{0:4d} {1:4d}'.format(numPrimesCalcd, candidate)
divTest = 3 #reset divTest to lowest possible divisor
candidate += 2 #skip even numbers
elif candidate%divTest == 0 :
# Not a prime, because it divides by a number other than itself
divTest = 3 #reset divTest to lowest possible divisor
candidate += 2 #skip even numbers
break #break out of the while loop, start testing a new candidate
else :
# Haven't proved or disproved, test with the next odd number
divTest += 2
###PS1b###
from math import *
numPrimesCalcd = 1 #because we include '2' manually
candidate = 3 #the first number to test
divTest = 3 #the minimum number a given prime might divide by
sumOfPrimesLog = log(2) #b/c we already counted 2 manually
while numPrimesCalcd < 1000 :
while divTest <= candidate :
if divTest == candidate :
# It's a prime, because it hasn't divided by anything until it divided by itself
numPrimesCalcd += 1
print sumOfPrimesLog/candidate
sumOfPrimesLog += log(candidate)
divTest = 3
candidate += 2 #skip even numbers
elif candidate%divTest == 0 :
# Not a prime, because it divides by a number other than itself
divTest = 3 #reset divTest to lowest possible divisor
candidate += 2 #skip even numbers
break #break out of the while loop, start testing a new candidate
else :
divTest += 2
Nicholas123
1 year ago
|
 |
|
|
|