projects
/
sicp.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
39073af
)
Adding solution to 1.17.
author
Ramakrishnan Muthukrishnan
<vu3rdd@gmail.com>
Fri, 9 Apr 2010 13:37:38 +0000
(19:07 +0530)
committer
Ramakrishnan Muthukrishnan
<vu3rdd@gmail.com>
Fri, 9 Apr 2010 13:37:38 +0000
(19:07 +0530)
chapter1/ch1_2.clj
patch
|
blob
|
history
diff --git
a/chapter1/ch1_2.clj
b/chapter1/ch1_2.clj
index d6bb552e34c09ab6b3dff6fbfc4828e6748cdca2..ed57832f345d68a84df1e2ae4f76770c9e6c3c5f 100644
(file)
--- a/
chapter1/ch1_2.clj
+++ b/
chapter1/ch1_2.clj
@@
-491,3
+491,23
@@
TRACE t2494: => -0.39980345741334
(even? n) (expt-iter (square b) (/ n 2) a)
:else (expt-iter b (- n 1) (* a b))))
+;; exercise 1.17:
+(defn mult [a b]
+ (if (= b 0)
+ 0
+ (+ a (mult a (- b 1)))))
+
+;; product = 2 * (a * (b/2)) for even b
+;; = a + (a * (b - 1)) for odd b
+(defn fast-mult [a b]
+ (cond (= b 0) 0
+ (= b 1) a
+ (even? b) (twice (fast-mult a (half b)))
+ :else (+ a (fast-mult a (- b 1)))))
+
+;; double
+(defn twice [x]
+ (* 2 x))
+
+(defn half [x]
+ (/ x 2))
\ No newline at end of file