Skip to content

Instantly share code, notes, and snippets.

@athos athos/pi.clj
Last active May 24, 2018

Embed
What would you like to do?
(defn a&b [n]
(loop [n n, a (/ 1.0 (* 2 (Math/pow 3 0.5))), b (/ 1.0 3)]
(if (= n 0)
[a b]
(let [a' (/ (+ a b) 2)]
(recur (dec n) a' (Math/pow (* a' b) 0.5))))))
;; => (time (/ 1 (first (a&b 100))))
;; "Elapsed time: 0.04547 msecs"
;; 3.141592653589795
;; =>
(declare b)
(defn ^:dynamic a [n]
(if (= n 0)
(/ 1 (* 2 (Math/pow 3 0.5)))
(/ (+ (a (- n 1)) (b (- n 1))) 2)))
(defn ^:dynamic b [n]
(if (= n 0)
(/ 1.0 3)
(Math/pow (* (a n) (b (- n 1))) 0.5)))
;; => (time
;; (binding [a (memoize a)
;; b (memoize b)]
;; (/ 1 (a 100))))
;; "Elapsed time: 1.764405 msecs"
;; 3.141592653589795
;; =>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.