#lang scheme
(define (even? n)
(= (remainder n 2) 0))
(define (double a )
(double-iter a 2 0))
(define (double-iter a counter product)
(if (= counter 0)
product
(double-iter a (- counter 1) (+ a product))))
(define (mod a div)
(- (/ a div) (/ 1 div)))
;; 1.18
(define (fast-multi-iter a counter product result )
(cond ((= counter 1) (+ product result ) )
(( even? counter) (fast-multi-iter a (/ counter 2) (double product) result ))
(else (fast-multi-iter a (mod counter 2) (double product) (+ result product ) ) )))
(define (* a b)
(fast-multi-iter a b a 0 )
)
;; 1.17
(define (fast-multi b n)
(cond ((= n 0) 0)
((even? n) (double (fast-multi b (/ n 2))))
(else (+ b (fast-multi b (- n 1))))))
ps. 1.18 을 답을 보니 나만 틀렸네 ㅡㅡ
잘못된 방법으로 풀었음 .
역시 난 ㅠㅠ
영어 어려워서 책 질렀음
댓글 없음:
댓글 쓰기