JesFine


Joined 2 years ago
Homeworks submitted:
Homework comments:
3
3

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 11% complete

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 7% complete

Submitted Assignments

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

pset1

## question #1
import math

first_prime = 2
max_prime_count = 1000
count = 1

next_prime = first_prime + 1
count = count + 1

while (count < max_prime_count):
    next_prime = next_prime + 2
    stop_value = math.sqrt(next_prime)

    is_prime = True
    value = 2

    #Check if the "prime" is really prime by dividing it by
    #  all integers up to its sqrt and looking for remainders
    while (value <= stop_value):
        if (next_prime%value == 0):
            is_prime = False
            value = stop_value+1
        else: value = value + 1

    #increment counter if it is prime
    if (is_prime):
        count = count + 1
        #print count, ': ', next_prime

print '1000th prime is ' + str(next_prime)


## question #2
from math import *

first_prime = 2
max_prime_count = raw_input('How many primes: ')
count = 1
log_sum = log(first_prime)

next_prime = first_prime + 1
count = count + 1
log_sum = log_sum + log(next_prime)

while (count < int(max_prime_count)):
    next_prime = next_prime + 2
    stop_value = sqrt(next_prime)

    is_prime = True
    value = 2

    #Check if the "prime" is really prime by dividing it by
    #  all integers up to its sqrt and looking for remainders
    while (value <= stop_value):
        if (next_prime%value == 0):
            is_prime = False
            value = stop_value+1
        else: value = value + 1

    #increment counter if it is prime, print out the sum of logs,
    # and the ratio of the latest prime to it
    if (is_prime):
        log_sum = log_sum + log(next_prime)
        count = count + 1
        ratio = next_prime/log_sum

print count, ': ', next_prime, log_sum, ratio

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

pset0

last  = raw_input('Enter your last name:\n**')
first = raw_input('Enter your first name:\n**')

print first
print last

JesFine 2 years ago
Structure and Interpretation of Computer Programs: Lesson 1, HW 1

1.1 10 12 8 3 6 none none 19 false 4 16 6 16

1.2 (Couldn't really read it, but if my numbers are correct):

(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))

1.4 (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)) The "if" will return the + operator if b is positive, thus the procedure returns a + b if b is positive. The "if" will return the - operator if b is negative (or zero), thus the procedure returns a - b. Subtracting a negative number is the same as adding the absolute value of that number, so this procedure will return the same value for a given and and either +/-b. Just like the name says.

1.5 In applicative order, the p is evaluated, but the definition is recursive, so it will continually be called, resulting in an infinite loop. In normal order, the p never gets evaluated because the first condition of the test is true, and thus returns 0.

1.6 Similar to the answer to 1.5, the recursive function is continuously evaluated before the test conditions are checked and an infinite loop condition is reached.

1.7 For small values, the result is off by a lot (ie, sqrt .00001 => .031356) and large values never finish. Changing to the new version gives a better value, for small numbers (ie sqrt .0001 => .00316) and large values actually finish.

1.8 procedure good-enough? doesn't change

*1.3*
(define (max-sum-of-squares a b c)
    (cond ((and (>= a c) (>= b c)) (+ (square a) (square b)))
          ((and (>= a b) (>= c b)) (+ (square a) (square c)))
          (else (+ (square b) (square c)))))

*1.7*
(define (sqrt x)
    (sqrt-iter 1.0 0 x))
(define (sqrt-iter guess old-guess x)
    (if (good-enough? guess old-guess)
        guess
        (sqrt-iter (improve guess x) guess x)))
(define (good-enough? guess old-guess)
    (< (abs (- (/ (- old-guess guess) guess))) .0001))

*1.8*
(define (cube-rt-iter guess old-guess x)
    (if (good-enough? guess old-guess)
        guess
        (cube-rt-iter (improve-cube guess x) guess x)))
(define (improve-cube guess x)
    (/ (+ (/ x (square guess)) (* 2 guess)) 3))
(define (cube-rt x)
    (cube-rt-iter 1.0 0.0 x))

JesFine 2 years ago