Last active
May 24, 2018 03:12
-
-
Save athos/1e4a8d9d0f96d9ecaf9041ae1f9e4fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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