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
# Problem Set 2
# Name: Hans Jacob T. Stephensen (Dith)
# Collaborators: None
# Time: XX:XX (But I've used far more than I feel I should)
# Problem ps2a.py
def nuggets(lower_limit, upper_limit):
'''Calculate possible solutions for the Diophantine Equation of:
"n = a*6 + b*9 + c*20" (using "exhausting enumeration")
where n is defined by the lower and upper limit
returns True or False if a solution is found'''
# Checks for parameters that wouldn't yield useful results
assert lower_limit <= upper_limit, 'the lower limit lies above the upper limit'
assert type(lower_limit*upper_limit) == int, 'an input value is not an integer'
assert lower_limit >= 0 and upper_limit >= 0, 'an input value is negative'
result = False
# Bruteforcing the three valiables by enumeration attempts, trying every possibility.
# Dividing by 6 to get worst case scenario and to improve efficiency
for c in range(int(upper_limit/20)+1):
for b in range(int(upper_limit/9)+1):
for a in range(int(upper_limit/6)+1):
n = 6*a+9*b+20*c
if n >= lower_limit and n <= upper_limit:
print 'Succes n =', n, 'With values a =', a, 'b =', b, 'c =', c
result = True
print 'done'
if result == False:
print 'No solutions between:', lower_limit, 'and', upper_limit
return True
else: return False
def test_a():
'''It't a test for the nuggets function'''
raw_input("Testing 50 to 55")
nuggets(50,55) #base test 1
raw_input("Testing 56 to 65")
nuggets(56,65) #base test 2
raw_input("Testing 16 to 17")
nuggets(16,17) #base test with no results
raw_input("")
nuggets(120,121)
raw_input("")
test_a()
# Problem ps2b.py
def nuggets_b(nu):
'''finds the highest value of n which lower than input value in:
"n = 6*a + 9*b + 20*c" by use of exhausting enumeration.
Note that values higher than 200 takes some seconds'''
n = abs(int(nu))
result = True
# Checks for parameters that wouldn't yield useful results
if type(nu) != int:
print 'value not integer! Converting:', nu, 'to the integer of:', n
if nu < 0:
print 'value is not positive! Using absolute value'
while result:
res = 0
for c in range(int(n/20)+1):
for b in range(int(n/9)+1):
for a in range(int(n/6)+1):
num = 6*a+9*b+20*c
if num == n:
res += 1
#Edited out: print 'Solution for n =', n, 'found!'
if res == 0:
result = False
print 'No solutions for n =', n, 'below the value of', nu
n -= 1
nuggets_b(200)
def test_b():
'''It't a test for the nuggets function'''
raw_input("")
nuggets_b(50)
raw_input("")
nuggets_b(55.5)
raw_input("")
nuggets_b(30)
raw_input("")
nuggets_b(-50)
raw_input("")
test_b()
Dith
9 months ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem Set 1
# Name: Hans Jacob T. Stephensen (Dith)
# Collaborators: None
# Time: 0:43 (Different attempts.. it's an ineffectient program. but it works)
primecount = 1 #variable to keep track of primes
number = 3 #variable to cycle through
while 1: #Unending loop - stopped by "break" later in the program
for i in range(2,number):
if number%i == 0: #Checks all possible devisors and break if one is found
#print number, 'is not prime'
break
else: #If break is not used this else is run ("stole" this trick from the python documentation)
primecount += 1
#print number, 'is prime', 'count:', primecount
if primecount == 1000:
print 'prime 1000: ', number
break
number += 1
Dith
9 months ago
|
 |
|
|
|