From f0ef016256924a3e47e075495560138f1f5a9721 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Tue, 18 May 2010 19:01:41 +0530 Subject: [PATCH] A much better way to generate the denominator sequence. --- src/sicp/ex1_38.clj | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/sicp/ex1_38.clj b/src/sicp/ex1_38.clj index 54acde7..07bb2e5 100644 --- a/src/sicp/ex1_38.clj +++ b/src/sicp/ex1_38.clj @@ -9,14 +9,17 @@ (concat [0 1] (interleave s1 s2 s2)))) ;; we concat [0 1] because nth sequences are indexed from 0 +(defn den-seq [k] + (if (= (rem k 3) 2) + (* (/ (+ k 1) 3) 2) ;; ((k+1)/3)*2 + 1)) + ;; approximating e (defn e-approximation [len] - (let [l (* 3 (int (/ len 3.0))) - den (gen-sequence l)] - (+ 2.0 - (cont-frac (fn [k] 1.0) - (fn [k] (nth den k)) - l)))) + (+ 2.0 + (cont-frac (fn [k] 1.0) + (fn [k] (den-seq k)) + len))) (comment user> (e-approximation 10) @@ -31,4 +34,5 @@ user> (e-approximation 60) 2.7182818284590455 user> (e-approximation 100) 2.7182818284590455 -) \ No newline at end of file +) + -- 2.45.2