About Me
No description provided.
Classes
|
Class status: Established
Role:
Student
|
.
11% complete
|
Submitted Assignments
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
Problem Set 2
I didn't have the ps2b_template.py, so I improvised. I think this should work just as well and solve the general case.
# Problem 3
# mcdio takes an integer, num, and determines if it has a solution
# to the equation 20a + 9b + 6c = num. If it does, it returns True.
# If not, it returns nothing.
def mcdio(num):
for i in range(0,num/20+1):
for j in range(0,num/9+1):
for k in range(0,num/6+1):
if 20*i + 9*j + 6*k == num:
return True
print i,j,k
break
# mcdioSoln starts at 0 and tests every integer x to determine
# if there is a solution to 20a + 9b + 6c = x. When 5 consecutive
# x's are found such that there is a solution to the equation, it
# returns the last x such that there is no such solution.
def mcdioSoln():
num = 0
cons = 0
bestFound = 0
while cons < 6:
num += 1
if mcdio(num) == True:
cons += 1
else:
cons = 0
bestFound = num
return bestFound
# Problem 4
# gen_mcdio will be called by gen_mcdio_search to determine if the
# current number being tested by gen_mcdio_search has a solution
# for the given a,b,c.
def gen_mcdio(a,b,c,n):
for i in range(0,n/c+1):
for j in range(0,n/b+1):
for k in range(0,n/a+1):
if c*i + b*j + a*k == n:
return True
# gen_mcdio_search iterates through the positive integers less than
# 200 and calls gen_mcdio to test if a number has a solution.
def gen_mcdio_search(num1,num2,num3):
cons = 0
bestFound = 0
while cons < 6:
for n in range(0,200):
n += 1
if gen_mcdio(num1,num2,num3,n):
cons += 1
else:
cons = 0
bestFound = n
return bestFound
# Example:
gen_mcdio_search(5,8,10) # returns 27
phaedrus
9 months ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem 1
def is_prime(n):
divisors = []
for i in range(1, n/2+1):
if n%i == 0:
divisors.append(i)
if divisors == [1]:
return True
else:
return False
def nth_prime(num):
current = 3
primesFound = 1
assert num != 1
while primesFound < num:
if is_prime(current):
if primesFound == num-1:
break
current += 2
primesFound += 1
else:
current += 2
return current
# Problem 2
from math import *
def prime_log_sums(num):
lSum = 0
for i in range(1, num,2):
if is_prime(i):
lSum = lSum + log(i)
lSum = lSum + log(2)
return lSum, num, lSum/num
phaedrus
9 months ago
|
|
|