15 hours ago
my attempt to do the exercises in sicp.
Thursday, June 26, 2008
sicp exercise 1.18
; Exercise 1.18. Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative
; process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic
; number of steps.
(define (double a) (+ a a))
(define (halve a) (/ a 2))
(define (fast-mul-iter a b result)
(cond ((= b 0) result)
((even? b) (fast-mul-iter (double a) (halve b) result ))
(else (fast-mul-iter a (- b 1) (+ a result)))))
(display (fast-mul-iter 10 9 0)) (newline)
(display (fast-mul-iter 99999 99999 0)) (newline)
(display (fast-mul-iter 9999999999999999999999999 9999999999999999999999999 0)) (newline)