1.19 Exercise 1.19
By expanding two successive transformations, we can find the values of p’ and q’:
a <- (bp + aq)q + (bq + a1 + ap)q + (bq + aq + ap)p |
= bp + aq^2 + bq^2 + aq^2 + apq + bpq + apq + ap^2 |
= b(2pq + q^2) + a(2q^2 + 2pq + p^2) |
= b(2pq + q^2) + a(2pq + q^2) + a(p^2 + q^2) |
Therefore, q’ = 2pq + q^2 and p’ = p^2 + q^2. We can use this to finish the procedure below:
(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 (+ (square p) (square q)) (+ (square q) (* 2 p q)) (/ count 2))) (else (fib-iter (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p q (- count 1)))))