Created
December 7, 2012 13:58
-
-
Save tnoda/4233420 to your computer and use it in GitHub Desktop.
Project Euler Problem 2 #mitori_clj
This file contains 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 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))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ypsilon-takai の書いた iterate 版は Programming Clojure の中でも紹介されている方法で、シンプルで Clojure らしいコードになっているので、すごく好きです。
Programming Clojure では、末尾再帰なし版、末尾再帰(
loop
recur
)版、lazy-seq
版と紹介され、最後が iterate による遅延シーケンスを返す版だったと思います。