Skip to content

Instantly share code, notes, and snippets.

@athos
Last active May 24, 2018 03:12
Show Gist options
  • Select an option

  • Save athos/1e4a8d9d0f96d9ecaf9041ae1f9e4fa7 to your computer and use it in GitHub Desktop.

Select an option

Save athos/1e4a8d9d0f96d9ecaf9041ae1f9e4fa7 to your computer and use it in GitHub Desktop.

Revisions

  1. athos revised this gist May 24, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions pi.clj
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,24 @@
    ;; => (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
    ;; =>
  2. athos created this gist May 24, 2018.
    11 changes: 11 additions & 0 deletions pi.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    (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
    ;; =>