Let’s celebrate Pi day in clojure!
Exactly 140 characters to print 9 digits of $\pi$ (3.141592654
)
(let[r reduce N(rest(range))t take](clojure.pprint/cl-format nil"~11f"
(r +' -2(map #(/(r *' 2(t % N))(r *'(t %(take-nth 2 N))))(t 99 N)))9))
There are two parts of a problem:
(defn pi
"Calculate PI by arctangent algorithm"
[& {:keys [iterations limit]
:or {iterations 99 limit 9}}]
(->> (range 1 (inc iterations))
(map #(/ (reduce *' (take % (iterate inc 1)))
(reduce *' (take % (filter odd? (iterate inc 1))))))
(reduce +')
(* 2)
(dec)
(dec)))
clojure.pprint/cl-format
(clojure.pprint/cl-format nil "~4f" 22/7) ;;=> "3.14"
To achieve 140 characters barrier, some functionality was relaxed
99
to control number of iterations for $\pi$ calculation (if you have a high number of iterations, be patient - wait)9
to increase number of digits (that makes sense if you improved number of iterations)P.S. Calculating $\pi$ and some other irrationals is available in numberto
mishadoff 14 March 2015