yanni79


Joined 1 year ago
Homeworks submitted:
Homework comments:
3
5

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 17% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1

This took a while to get working. I'll be curious to see how other people tested for consecutive values.

Problem 1 (worked out manually)

55: 2c + 1a + 1b 54: 6b 53: 4a + 1b + 1c 52: 2a + 2c 51: 1a + 5b 50: 5a + 1c


56 = 50 + 6 | 50 = 5a + 1c | 6 = 1a 56 = 6a + 1c + 1a


57 = 51 + 6 | 51 = 1a + 5b | 6 = 1a 57 = 1a +5b + 1a


58 = 52 + 6 | 52 = 2a + 2c | 6 = 1a 58 = 2a + 2c + 1a


59 = 53 + 6 | 53 = 4a + 1b + 1c | 6 = 1a 59 = 4a + 1b + 1c + 1a = 5a +1b + 1c


60 = 54 + 6 | 54 = 6b | 6 = 1a

61 = 55 + 6 | 55 = 2c + 1a + 1b | 6 = 1a

62 = 50 + 12 | 50 = 5a + 1c | 12 = 2a

63 = 51 + 12 | 1a + 5b | 12 = 2a

64 = 58+ 6 | 58= 2a + 2c + 1a | 6 = 1a 64 = 2a + 2c + 1a + 1a = 4a+2c


65 = 59 + 6 | 59 = 5a +1b + 1c | 6 = 1a 65 = 6a +1b+1c

Problem 2:

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.

This can be shown to be true because we’ve shown how to “add” up for every integer lower than the lowest “McNugget” packet (6). We can then derive all other known digits, by adding 6 to the existing proofs. SO that x+6 is gives you x+6, x+1 +6 gives you x+7, and x+2+6 gives you x+8. Deriving all the possible digits, proves that any number (or multiple) of those digits can also be derived.

#testing for maximum n where 6a+9b+20c =! n  PROBLEM #3
impossible_Order = [0,] #collection of impossible order (aka n)
#successful impossible_Order should be (1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, etc)
possible_Order = [0, 0, 0, 0, 0, 0]
#collection of values that have a possible solution (not sure if needed)
#prepopulate the first 6 impossible values for calculation reasons
n = 1 #possible instances of numbers of McNuggets that cannot be purchased exactly
max_n = 100 #this is the biggest n we're going to try. 

max_multiplier = int(max_n / 6) #6 is the lowest "denominator" that will fit into n. 

def test_consecutive_values(): #this function will add up the last 6 numbers in possible_Order to see if we've found the solution
    sum_of_values = 0
    if possible_Order[-1] - 1 == possible_Order[-2] and possible_Order[-1] - 2 == possible_Order[-3] and possible_Order[-1] - 3 == possible_Order[-4] and possible_Order[-1] - 4 == possible_Order[-5] and possible_Order[-1] - 5 == possible_Order[-6]:
    #the if statement above tests to see if the orders are consecutive. There is probably a more elegant way of doing this
        return True
    else:
        return False
      
while n < max_n and test_consecutive_values() != True:
    for a in range(max_multiplier):
        for b in range(max_multiplier):
            for c in range(max_multiplier):
                if (6*a + 9*b +20*c == n) and possible_Order[-1] != n:
                    #the above tests n as a possible solution, and also verifies that other solutions (of a,b,c for n weren't found
                    possible_Order.append(n)                   
                    break               
    if possible_Order[-1] != n: #test to see if a possible solution to n has been found. If not, make it an impossible solution.
        impossible_Order.append(n)
    n += 1
possible_Order = possible_Order[6:] # just cleanup since this was preinitialized with six zeroes. 
print "Largest number of McNuggets that cannot be bought in exact quantity: ", impossible_Order[-1]

#the end of problem 3

#########################################
#testing for maximum n where 6a+9b+20c =! n  PROBLEM #4
bestSoFar = 0
packages = (6,9,20)
possible_Order = [0, 0, 0, 0, 0, 0]
#collection of values that have a possible solution (not sure if needed)
#prepopulate the first 6 impossible values for calculation reasons
n = 1 #possible instances of numbers of McNuggets that cannot be purchased exactly
max_n = 200 #this is the biggest n we're going to try. 

max_multiplier = int(max_n / packages[-3]) #divide by the lowest "denominator" that will fit into n. 

def test_consecutive_values(): #this function will add up the last 6 numbers in possible_Order to see if we've found the solution
    sum_of_values = 0
    if possible_Order[-1] - 1 == possible_Order[-2] and possible_Order[-1] - 2 == possible_Order[-3] and possible_Order[-1] - 3 == possible_Order[-4] and possible_Order[-1] - 4 == possible_Order[-5] and possible_Order[-1] - 5 == possible_Order[-6]:
    #the if statement above tests to see if the orders are consecutive. There is probably a more elegant way of doing this
        return True
    else:
        return False

for n in range(1, max_n):
    if test_consecutive_values() == True:
        break
    for a in range(max_multiplier):
        for b in range(max_multiplier):
            for c in range(max_multiplier):
                if (packages[-3]*a + packages[-2]*b +packages[-1]*c == n) and possible_Order[-1] != n:
                    #the above tests n as a possible solution, and also verifies that other solutions (of a,b,c for n weren't found
                    possible_Order.append(n)                   
                    break               
    if possible_Order[-1] != n: #test to see if a possible solution to n has been found. If not, make it an impossible solution.
        bestSoFar = n
possible_Order = possible_Order[6:] # just cleanup since this was preinitialized with six zeroes. 
print "Given package sizes %d, %d, and %d, the largest number of McNuggets that cannot be bought in exact quantity is: %d" % (packages[-3], packages[-2], packages[-1], bestSoFar)

#the end of problem 4



yanni79 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1

Please provide some feedback?

In order to "expedite" the prime calculations, I only try to divide them by odd numbers, and stop at values <= the square root of the candidate. What other numbers can I eliminate?

##Part 1
from math import *
list_of_candidates = range(5, 10000, 2) ##create a list with odd numbers
prime_numbers = []  #this is going to be my collection of generated prime numbers

for candidate in list_of_candidates: #go through all odd numbers 
    if len(prime_numbers) == 1000: break #test the # of prime numbers found
    divisor = 3 #this is the first denominator we're going to start with
    while divisor < candidate and divisor <= sqrt(candidate): #test denominators less than the candidate, and <= its square root
        if candidate % divisor == 0: break #this must not be a prime number
        elif candidate % divisor > 0: #the denimator doesn't go into the candidate evenly
            divisor = divisor + 2 #increment the denominator, but stay with odd denominators
    else: prime_numbers.append(candidate) #reaching this statement requires the number to be prime

prime_numbers.insert(0, 2) #adding the missing prime 2 to our final list
prime_numbers.insert(1, 3) #adding the missing prime 3 to our final list


print "The 1000's prime number is ", prime_numbers[999] #Final output to user

###########PART 2 (continuation of part 1)

numberN = int(raw_input(["Enter a number less than ", prime_numbers[999]])) #ask to use a number less than the biggest prime stored
i = 0 #define our itterator
sum_of_logs = 0 #set the initial sum
while int(prime_numbers[i]) < numberN: #test if we've reached the user number
    sum_of_logs = sum_of_logs + log(prime_numbers[i]) #keep adding until we reach the user specified number
    i = i + 1 #increment itterator
print "your number: ", numberN, "\nthe sum of all primes below is", sum_of_logs, "\ntheir ratio is", sum_of_logs/numberN

yanni79 1 year ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 1, HW 1
last_name = raw_input('Last name? \n**')
first_name = raw_input('First name? \n**')
print first_name, last_name

yanni79 1 year ago