projects
/
sicp.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
3853351
)
Added solution to 1.19.
author
Ramakrishnan Muthukrishnan
<vu3rdd@gmail.com>
Sat, 10 Apr 2010 20:12:38 +0000
(
01:42
+0530)
committer
Ramakrishnan Muthukrishnan
<vu3rdd@gmail.com>
Sat, 10 Apr 2010 20:12:38 +0000
(
01:42
+0530)
chapter1/ch1_2.clj
patch
|
blob
|
history
diff --git
a/chapter1/ch1_2.clj
b/chapter1/ch1_2.clj
index 0cf3c556e8e2dc15024fe9eeec1caaa12bf815e8..13b99d1f881433a0e844eb8d88cf03bd6fbff1a1 100644
(file)
--- a/
chapter1/ch1_2.clj
+++ b/
chapter1/ch1_2.clj
@@
-536,3
+536,22
@@
TRACE t2916: | => 6
TRACE t2915: => 6
6
)
+
+;; exercise 1.19: fast fibonacci
+;; see the pdf of the notebook scan for the derivation of p' and q'
+(defn ffib [n]
+ (ffib-iter 1 0 0 1 n))
+
+(defn ffib-iter [a b p q count]
+ (cond (= count 0) b
+ (even? count)
+ (ffib-iter a
+ b
+ (+ (* p p) (* q q))
+ (+ (* 2 p q) (* q q))
+ (/ count 2))
+ :else (ffib-iter (+ (* b q) (* a q) (* a p))
+ (+ (* b p) (* a q))
+ p
+ q
+ (- count 1))))