Skip to content

Instantly share code, notes, and snippets.

@tnoda
Created December 7, 2012 13:58
Show Gist options
  • Save tnoda/4233420 to your computer and use it in GitHub Desktop.
Save tnoda/4233420 to your computer and use it in GitHub Desktop.
Project Euler Problem 2 #mitori_clj
(defn fib
"Returns the nth Fibonacci number."
^long
[n]
(-> (* 0.5 (inc (Math/sqrt 5))) ; golden ratio
(Math/pow n)
(/ (Math/sqrt 5))
(+ 0.5)
Math/floor
long))
(loop [n 0 sum 0]
(let [x (fib n)]
(if (< x 4000000)
(recur (+ n 3) (+ sum x))
sum)))
@bouzuya
Copy link

bouzuya commented Jan 13, 2013

@ypsilon-takai の書いた iterate 版は Programming Clojure の中でも紹介されている方法で、シンプルで Clojure らしいコードになっているので、すごく好きです。

Programming Clojure では、末尾再帰なし版、末尾再帰(loop recur)版、lazy-seq 版と紹介され、最後が iterate による遅延シーケンスを返す版だったと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment