## Solution to SICP Exercise 1.36

Solution to Exercise 1.36:

```(define tolerance 0.00001) (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (display guess) (newline) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess))```

``` (define (average a b) (/ (+ a b) 2)) (define result-with-damping (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 2.0)) (display "result-with-damping: ") (display result-with-damping) (newline) (newline) (define result-without-damping (fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0)) ```

```(display "result-without-damping: ") (display result-without-damping) ```

Output:

```2.0 5.9828921423310435 4.922168721308343 4.628224318195455 4.568346513136242 4.5577305909237005 4.555909809045131 4.555599411610624 4.5555465521473675 result-with-damping: 4.555537551999825```

``` ```

```2.0 9.965784284662087 3.004472209841214 6.279195757507157 3.759850702401539 5.215843784925895 4.182207192401397 4.8277650983445906 4.387593384662677 4.671250085763899 4.481403616895052 4.6053657460929 4.5230849678718865 4.577114682047341 4.541382480151454 4.564903245230833 4.549372679303342 4.559606491913287 4.552853875788271 4.557305529748263 4.554369064436181 4.556305311532999 4.555028263573554 4.555870396702851 4.555315001192079 4.5556812635433275 4.555439715736846 4.555599009998291 4.555493957531389 4.555563237292884 4.555517548417651 4.555547679306398 4.555527808516254 4.555540912917957 result-without-damping: 4.555532270803653 ```

## Solution to SICP Exercise 1.35

Solution to Exercise 1.35:

```(define golden-ratio (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0))```

``` ```

```> golden-ratio 1.6180327868852458 ```

## Solution to SICP Exercise 1.34

Solution to Exercise 1.34:

DrScheme prints out an error message that reads:

`procedure application: expected procedure, given: 2; arguments were: 2`.

This is hardly surprising since `(f f)` evaluates to `(f 2)`, which evaluates to `(2 2)`, which is nonsense. The number `2` is not a procedure.

## Solution to SICP Exercise 1.33

Solution to Exercise 1.33:

```(define (filtered-accumulate predicate combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (if (predicate a) (combiner (term a) result) result)))) (iter a null-value))```

``` ; part a (define (square x) (* x x)) (define (sum-of-squared-primes a b) (define (square-and-add i result) (+ (square i) result)) (filtered-accumulate prime? square-and-add 0 identity a inc b)) ```

```; part b (define (product-of-positives-relatively-prime-to n) (define (relatively-prime? i) (= 1 (gcd i n))) (filtered-accumulate relatively-prime? * 1 identity 1 inc (- n 1))) ```

## Solution to SICP Exercise 1.32

Solution to Exercise 1.32:

`; part a (recursive process)`

``` (define (accumulate-r combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate-r combiner null-value term (next a) next b)))) (define (sum-r term a next b) (accumulate-r + 0 term a next b)) (define (product-r term a next b) (accumulate-r * 1 term a next b)) ; part b (iterative process) ```

```(define (accumulate-i combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combiner result (term a))))) (iter a null-value)) ```

## Solution to SICP Exercise 1.31

Solution to Exercise 1.31:

```; part a (recursive process) (define (product-r term a next b) (if (> a b) 1 (* (term a) (product-r term (next a) next b))))```

``` (define (identity i) i) (define (inc i) (+ i 1)) (define (factorial n) (product-r identity 1 inc n)) (define (pi-approximator terms) (define (numerator i) (+ i (if (odd? i) 1 2))) (define (denominator i) (+ i (if (odd? i) 2 1))) (define (pi-term i) (/ (numerator i) (denominator i))) (* 4.0 (product-r pi-term 1 inc terms))) ```

```; part b (iterative process) (define (product-i term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* result (term a))))) (iter a 1)) ```

## Solution to SICP Exercise 1.30

Solution to Exercise 1.30:

```(define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ result (term a))))) (iter a 0)) ```

## Solution to SICP Exercise 1.29

Solution to Exercise 1.29:

```(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))```

``` (define (inc n) (+ n 1)) ```

```(define (simpsons-integral f a b n) (define (do-it h) (define (y k) (f (+ a (* k h)))) (define (simpson-term k) (* (y k) (cond ((or (= k 0) (= k n)) 1) ((odd? k) 4) (else 2)))) (* (/ h 3) (sum simpson-term 0 inc n))) (do-it (/ (- b a) n))) ```

## Solution to SICP Exercise 1.28

Solution to Exercise 1.28:

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

``` (define (expmod-with-trivial-sqrt-check base exp m) (cond ((= exp 0) 1) ((even? exp) (let* ((intermediate (expmod-with-trivial-sqrt-check base (/ exp 2) m)) (squared-mod (remainder (square intermediate) m))) (if (and (not (or (= intermediate 1) (= intermediate (- m 1)))) (= squared-mod 1)) 0 squared-mod))) (else (remainder (* base (expmod-with-trivial-sqrt-check base (- exp 1) m)) m)))) ```

```(define (miller-rabin-test n) (define (try-it a ) (= (expmod-with-trivial-sqrt-check a (- n 1) n) 1)) (try-it (+ 1 (random (- n 1)))))```

## Congratulations Team Smartass

On Friday, the Google Research blog that Team Smartass, a group of Google employees, had won the ICFP Programming Contest, an annual 3-day event usually held over the Labour Day weekend. No big surprise there. We all know that Google only hires the brightest programmers on the planet.

I was happy to see that among the members of the winning team was one Chris Hendrie. Chris was a coworker of mine back when I worked at Texas Instruments; a great guy, and one of the smartest people I’ve ever known. He was definitely in another league from the rest of us slogs.

Anyways, I’m glad to see that he’s accomplished something that he talked about even back then. I always knew he was capable of it. He just needed at more capable team. It seems he’s found it.

Congratulations Chris and Team Smartass!