[ prog / sol / mona ]

prog


Help on the Way

86 2022-06-25 19:58

>>83-85
Thank you for your help. I believe I have found a solution:

(define (fast-expt-iter b n a)
  (cond ((= n 0) a)
        ((even? n) (fast-expt-iter (square b) (/ n 2) a))
        (else (fast-expt-iter b (- n 1) (* a b)))))

Observing if n is even:
```b^n = (b^2)^n/2```
and do:
```(b, n, a) -> (b^2, n/2, a)```

Observing if it is not:
```b^n = b*b^n-1```
and do:
```(b, n, a) -> (b, n-1, a*b)```

Where it is seen:

   (fast-expt-iter 2 6 1)
-> (fast-expt-iter 4 3 1)
-> (fast-expt-iter 4 2 4) 
-> (fast-expt-iter 16 1 4)
-> (fast-expt-iter 16 0 64)
-> 64

I was a bit lost when >>85-san went on about deconstructing the bits, but I will thank the good explanation of the state transformation for helping me work this out.
Orders of growth and the nature of these iterative processes are still a bit arcane to me, but I feel as though the fog has lifted. Thank you!

103


VIP:

do not edit these