Skip to content

Instantly share code, notes, and snippets.

@RussellAndrewEdson
Last active August 29, 2015 14:12
Show Gist options
  • Save RussellAndrewEdson/121f85618abd94405c81 to your computer and use it in GitHub Desktop.
Save RussellAndrewEdson/121f85618abd94405c81 to your computer and use it in GitHub Desktop.
Clojure code to generate the nth Fibonacci number using a formula.
(require '[clojure.math.numeric-tower :as math])
(defn fibonacci
"Returns the nth Fibonacci number by using the Golden Ratio formula."
[n]
(let [root-five (math/sqrt 5M)
golden-ratio (/ (+ 1 root-five) 2)]
(math/round (bigdec (/ (math/expt golden-ratio n)
root-five)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment