From ce74f5c80b1ee500e35ac6bf669cbe17f51beed0 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Tue, 18 May 2010 18:49:00 +0530
Subject: [PATCH] Solution to 1.38. I am generating the denominator in a little
 convoluted way. There should be a better mathematical way to generate the
 denominator sequence.

---
 src/sicp/ex1_38.clj | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 src/sicp/ex1_38.clj

diff --git a/src/sicp/ex1_38.clj b/src/sicp/ex1_38.clj
new file mode 100644
index 0000000..54acde7
--- /dev/null
+++ b/src/sicp/ex1_38.clj
@@ -0,0 +1,34 @@
+(ns sicp.ex1_38
+  (:use [sicp utils ex1_37]
+	[clojure.contrib test-is]))
+
+(defn gen-sequence [len]
+  (let [l (+ 1 (int (/ len 3)))
+	s1 (map #(* % 2) (range 1 (inc l)))
+	s2 (map (fn [_] 1) (range 1 (inc l)))]
+    (concat [0 1] (interleave s1 s2 s2))))
+;; we concat [0 1] because nth sequences are indexed from 0 
+
+;; 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))))
+
+(comment
+user> (e-approximation 10)
+2.718283582089552
+user> (e-approximation 20)
+2.7182818284590278
+user> (e-approximation 40)
+2.7182818284590455
+user> (e-approximation 50)
+2.7182818284590455
+user> (e-approximation 60)
+2.7182818284590455
+user> (e-approximation 100)
+2.7182818284590455
+)
\ No newline at end of file
-- 
2.45.2