Sunday, June 22, 2008

This is for excercise 1.16:



(defun fast_expt (b counter limit product) (if (= counter limit) product (if (leq (* counter 2) limit) (fast_expt b (* counter 2) limit (* product product)) (fast_expt b (+ counter 1) limit (* product b)))))
FAST_EXPT
COMMON-LISP-USER> (setf res 2)
2
COMMON-LISP-USER> (fast_expt 2 1 7 res)
128
COMMON-LISP-USER> (fast_expt 2 1 8 res)
256
COMMON-LISP-USER> (fast_expt 2 1 9 res)
512
COMMON-LISP-USER> (fast_expt 2 1 10 res)
1024
COMMON-LISP-USER> (fast_expt 2 1 11 res)
2048

No comments: