Mar 17, 2008

From recursion to iteration.

Exercise 1.16 of 1.2.4 Exponentation in SICP.

Recursion.

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

(define (even? n)
(= (remainder n 2) 0))

(define (square n)
(* n n))


How do I rewrite this code to iterative process?

No comments: