Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@athos
Last active May 24, 2018 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athos/1e4a8d9d0f96d9ecaf9041ae1f9e4fa7 to your computer and use it in GitHub Desktop.
Save athos/1e4a8d9d0f96d9ecaf9041ae1f9e4fa7 to your computer and use it in GitHub Desktop.
(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