excercise-1.18

Exercise 1.18

Using the results of Exercise 1.16 and Exercise 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.

Ans

1
2
3
4
5
6
7
8
9
10
11
12
(define (double n)
(+ n n))

(define (half n)
(/ n 2))

(define (multi a b)
(define (multi-iter a b ans)
(cond ((= b 0) ans)
((even? b) (multi-iter (double a) (half b) ans))
(else (multi-iter a (- b 1) (+ a ans)))))
(multi-iter a b 0))