npolyspace


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

About Me

No description provided.

Classes

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 15% complete

Submitted Assignments

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

exercise 1.9

Recursive: 1: (+ 4 5) -> (inc (+ 3 5)) -> (inc (inc (+ 2 5))) -> (inc (inc (inc (+ 1 5)))) -> (inc (inc (inc (inc (+ 0 5))))) then start evaluating + and inc.

tail-recursive 2: (+ 4 5) -> (+ 3 6) -> (+ 2 7) -> (+ 1 8) -> (+ 0 9)=9.

Exercise 1.10. The following procedure computes a mathematical function called Ackermann's function.

(A 1 10) 1024

(A 2 4) 65536

(A 3 3) 65536

(define (f n) (A 0 n)) ; (f n) is (* n 2)

(define (g n) (A 1 n)) ; (g n) is (2^n)

(define (h n) (A 2 n)) ; (h n) is the expression 2 raised to the power 2 raised to the power ... raised to the power 2. (n occurrences of raising to the power 2)

Exercise 1.11.

Exercise 1.12.

Exercise 1.13. Prove that Fib(n) is the closest integer to n/5, where = (1 + 5)/2. Hint: Let = (1 - 5)/2. Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that Fib(n) = (n - n)/5.

following hint: first note that ((1+sqrt(5))/2)^(n+1)=1/4(1+sqrt(5)^(n-1)(1+5+2sqrt(5))/2^(n-1) and ((1-sqrt(5))/2)^(n+1)=1/4(1-sqrt(5)^(n-1)(1+5-2sqrt(5))/2^(n-1) phi^n-psi^n+ psi^(n-1)-psi(n-1)= (0.25(1+ 1/2+1/2sqrt(5))+0.25(1+ 1/2-1/2sqrt(5))) /2^(n-1)=phi^(n+1)-psi^(n+1)

Exercise 1.14. Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

(count-change 11) (cc 11 5) (+ (cc 11 4) (cc -39 5)) (+ (cc 11 3) (cc -14 4)) (+ (cc 11 2) (cc 1 3)) (+ (+(cc 11 1) (cc 6 2)) (+ (cc 1 2) (cc -9 3))) (+ (+(cc 11 0) (cc 10 1)) (+ (cc 6 1) (cc 1 2)) (+ (cc 1 1) (cc -4 2)))
(+ (+(cc 10 0)(cc 9 1)) (+(cc 6 0)(cc 5 1) (+(cc 0 2)(cc 1 1)) 1)) (+ (+(cc 9 0)(cc 8 1)) (+(cc 5 0)(cc 4 1) 1 1)) (+ (+(cc 8 0)(cc 7 1)) (+(cc 4 0)(cc 3 1) 1 1)) (+ (+(cc 7 0)(cc 6 1)) (+(cc 3 0)(cc 2 1) 1 1)) (+ (+(cc 6 0)(cc 5 1)) (+(cc 2 0)(cc 1 1) 1 1)) (+ (+(cc 5 0)(cc 4 1)) (+(cc 1 0)(cc 0 1) 1 1)) (+ (+(cc 4 0)(cc 3 1)) (+0 1) 1 1)) (+ (+(cc 3 0)(cc 2 1)) 1 1 1)) (+ (+(cc 2 0)(cc 1 1)) 1 1 1)) (+ (+0 1 ) 1 1 1)) (+ 1 1 1 1))

Exercise 1.15. The sine of an angle (specified in radians) can be computed by making use of the approximation sin x x if x is sufficiently small, and the trigonometric identity

to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered ``sufficiently small'' if its magnitude is not greater than 0.1 radians.) These ideas are incorporated in the following procedures:

a. How many times is the procedure p applied when (sine 12.15) is evaluated? sine recurses down by factors of 3 to 0.1 so sine is evaluated at each of: 12.15,4.05,1.35,0.45,0.15,0.05 values. So P is evaluated six times.

b. What is the order of growth in space and number of steps (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated? the order of growth is log(a) for the number of steps. The size is likely to also be order log(a), because tail call optimization will not be able to remove the call to p on the return of the recursive call.

Exercise 1.17. I suppose calling this fast-mult is misleading. I put in an if case to handle negative numbers.

1.19 T_pq(a,b)-> bq+aq+ap,bp+aq so TT-> (bp+aq)q+(bq+aq+ap)(q+p), (bp+aq)p+(bq+aq+ap)q = pq(b+b+a+a)+qq(a+b+a)+app, pq(a+a)+ppb+qq(b+a) =a(2pq+2qq+pp)+b(2pq+qq), a(2pq+qq)+b(pp+qq) =b(2pq+qq)+a(2pq+2qq)+a(pp+qq),b(pp+qq)+a(2pq+2qq) so q'=2pq+qq, p'=pp+qq

exercise 1.21 Use the smallest-divisor procedure to find the smallest divisor of each of the following numbers: 199, 1999, 19999. I got 199,1999,7

;Exercise 1.10. 
(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))


;Exercise 1.11. 
(define (ex1p11r n)
  (cond ((< n 3) n)
        (else (+ (ex1p11r (- n 1))
                 (* 2 (ex1p11r (- n 2)))
		 (* 3 (ex1p11r (- n 3)))))))		 


(define (ex1p11i n)
(if (< n 3) n	
  (ex1p11-iter 0 1 2 n)))

(define (ex1p11-iter a b c count)
  (if (= count 2)
      c
      (ex1p11-iter b c (+ c (* 2 b) (* 3 a)) (- count 1))))


Exercise 1.12. 
(define (pascal r c)
(cond ((= c 1) 1)
((= r c) 1)
(else (+ (pascal (- r 1) c) (pascal (- r 1) (- c 1))))))



(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine angle)
   (if (not (> (abs angle) 0.1))
       angle
       (p (sine (/ angle 3.0)))))


;Exercise 1.17. 
(define (double x)(* x 2))
(define (halve x)(/ x 2))
(define (fast-mult a b)
  (cond ((= a 0) 0)
        ((even? a) (double (fast-mult (/ a 2) b)))
        (else (if (> a 0) (+ b (fast-mult (- a 1) b))
		  (- (fast-mult (+ a 1) b) b)))))

;1.19
(define (fib n)
  (fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
  (cond ((= count 0) b)
        ((even? count)
         (fib-iter a
                   b
                   (+ (* p p)   (* q q))      ; compute p'
                   (+ (* 2 p q) (* q q))      ; compute q'
                   (/ count 2)))
        (else (fib-iter (+ (* b q) (* a q) (* a p))
                        (+ (* b p) (* a q))
                        p
                        q
                        (- count 1)))))


;exercise 1.21 
(define (square x) (* x x))
(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
  (= (remainder b a) 0))




npolyspace 2 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)) ;7+12=19 (= a b) ;#f (if (and (> b a) ( b a) b a)) ;6 ( (cond ((> a b) a) ;44=16 (( x y) (if (> x z) x (if (> y z) y z)) (if (> y z) y z))) (define (middle x y z) (- (+ x y z) (+ (largest x y z) (- 0 (largest (- 0 x) (- 0 y) (- 0 z)))))) (define (ex1p3 x y z) (+ (square (largest x y z)) (square (middle x y z))))

exercise 1.4 (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)) ;the (if (> b 0) + -) produces a function either + or - so that the correct operation will be done to combine a and b. If b is greater than 0, then a and b are added, otherwise b is subtracted from a which is equivalent to adding the abs of b.

Exercise 1.5. What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

Because evaluation of p is an infinite loop, under applicative order evaluation, this will not terminate, because evaluation of test implies evaluating each argument. Under normal order evaluation p will never be evaluated on (test 0 (p)). Under normal order evaluation, evaluation occurs inside the function definition on each occurrence of the argument. Because of the assumption about if, the else clause is not reached.

Exercise 1.6. What happens when Alyssa attempts to use this to compute square roots? Explain.

Both the then clause and if clause get evaluated, so the iteration will actually recurse indefinitely. (infinite loop)

Exercise 1.7. The problem with good-enough is that the tolerance is fixed at 0.001. So we can't get an answer to accuracy greater than 0.001 so if x is small, then there is a large relative tolerance for error on the answer. At very large numbers there will be rounding.

so (nsqrt 0.0000001) is 0.03125 and (nsqrt 0.0001) is 0.03231 (I said nsqrt because scheme didn't seem to let me redefine sqrt.)

(nsqrt (expt 10.0 50)) gets into an infinite loop because when you get to a guess of 10^25 and the numerical squaring doesn't give you exactly 10^50, you don't have more significant figures to add, and there isn't enough precision to meet the stopping tolerance.

Following the directions to track the changes in the guess, here is an improved version of sqrt-iter that does solve both problems, (at least the examples I mentioned earlier): (This implementation does have a small performance penalty, in that the guess is computed twice. My excuse is that we haven't covered let yet, and the corresponding lambda implementation of let would make this pretty difficult to read at this point.)

(define (sqrt-iter guess x) (if (<= (abs (- guess (improve guess x))) (* 0.0001 guess)) guess (sqrt-iter (improve guess x) x)))

Exercise 1.8. since square root used:

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

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

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

(define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001))

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

(define (nsqrt x) (sqrt-iter 1.0 x))

I now define for cube root:

(define (cube-good-enough? guess x) (< (abs (- (* guess (square guess)) x)) 0.001))

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

(define (cube-iter guess x) (if (cube-good-enough? guess x) guess (cube-iter (cube-improve guess x) x)))

(define (ncubert x) (cube-iter 1.0 x))




npolyspace 2 years ago