zephon


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

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

...

#Problem 1

number_of_primes = 0
candidate = 0
test = 0
remainder = 0

while (1):

    candidate = candidate + 1

    #test primality
    #1 if prime, 0 if not
    for test in range(candidate):
        remainder = (candidate)%(test + 1)
        if remainder == 0 and (test + 1) <> 1 and (test + 1) <> candidate:
            #not prime
            break
        if (test + 1) == candidate and candidate <> 1:
            number_of_primes = number_of_primes + 1


    if number_of_primes == 1000:
        print 'The %dth prime is %d' % (number_of_primes, candidate)
        break

#Problem 2

from math import *

number_of_primes = 0
candidate = 0
test = 0
remainder = 0
total = 0
ratio = 0

while (1):

    candidate = candidate + 1

    #test primality
    #1 if prime, 0 if not
    for test in range(candidate):
        remainder = (candidate)%(test + 1)
        if remainder == 0 and (test + 1) <> 1 and (test + 1) <> candidate:
            #not prime
            break
        if (test + 1) == candidate and candidate <> 1:
            #prime
            total = total + log(candidate)
            number_of_primes = number_of_primes + 1
            

    if candidate == 1000:
        ratio = total / candidate
        print 'n is %d' % (candidate)
        print 'Sum of log primes is %e' % (total)
        print 'Ratio is %e' % (ratio)
        break


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

Asks for last name, asks for first name, prints out first followed by last name.

last_name = raw_input('Enter your last name:\n')
first_name = raw_input('Enter your first name:\n')
print first_name
print last_name

zephon 3 years ago
Structure and Interpretation of Computer Programs: Lesson 1, HW 1

Exercise 1.1

10 10 (+ 5 3 4) 12 (- 9 1) 8 (/ 6 2) 3 (+ ( 2 4) (- 4 6)) 6 (define a 3) (define b (+ a 1)) (+ a b ( a b)) 19 (= a b) false (if (and (> b a) ( (cond ((= a 4) 6) ((= b 4) (+ 6 7 a)) (else 25)) 16 (+ 2 (if (> b a) b a)) 6 (* (cond ((> a b) a) ((< a b) b) (else -1)) (+ a 1)) 16

Exercise 1.4

This procedure adds the absolute value of b to the value of a. If b is positive, it adds a and b. If b is negative it subtracts b from a.

Exercise 1.5

Normal order evaluation will cause the program to return 0, because test will be evaluated before p is expanded. Applicative order evaluation will cause the interpreter to attempt to expand all the functions before evaluating them. Since p calls itself in an infinite loop, this program will never halt if evaluated by an applicative order interpreter.

Exercise 1.6

If the interpreter is using applicative order evaluation the program will never halt. This is because in the original program, if would have forced the interpreter to evaluate good-enough? which would eventually return true and halt the program, but with new-if, sqrt-iter is endlessly expanded and good-enough? is never evaluated.

Exercise 1.7

good-enough? tests the guess to within 0.001 of x, so if x is significantly less than 0.001 good-enough? will return true even if the guess is not close to the true root of x. If the number is large it will be dominated by rounding error, and good-enough? will cause the program to continue to attempt improve guess to within 0.001 of x even if guess is not approaching x because of error.

(code)

The updated program only returns guess when guess is relatively stable from one iteration of the program to the next, it will therefore be at least as accurate for small and large numbers as it is for medium sized ones.

Exercise 1.2

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

Exercise 1.3

(define (a_procedure x y z)
    (define (square x)
      (* x x))
  (cond ((and (>= x z) (>= y z)) (+ (square x) (square y)))
        ((and (>= x y) (>= z y)) (+ (square x) (square z)))
        ((and (>= y x) (>= z x)) (+ (square y) (square z)))
        ))

Exercise 1.7

(define (new-if predicate then-clause else-clause)
  (cond (predicate then-clause)
        (else else-clause)))

(define (square x)
  (* x x))

(define (average x y)
  (/ (+ x y) 2))

(define (improve guess x)
  (average guess (/ x guess)))

(define (good-enough? old-guess new-guess)
  (< (abs (- old-guess new-guess)) 0.001))

(define (sqrt-iter old-guess new-guess x)
  (new-if (good-enough? old-guess new-guess)
      new-guess
      (sqrt-iter new-guess (improve new-guess x)
                 x)))

(define (new-sqrt x)
  (sqrt-iter 1.0 2.0 x))

Exercise 1.8

# As above except:

(define (improve guess x)
  (/ (+ (/ x (square guess)) (* 2 guess)) 3))

zephon 3 years ago