Last active
August 29, 2015 14:12
-
-
Save RussellAndrewEdson/121f85618abd94405c81 to your computer and use it in GitHub Desktop.
Clojure code to generate the nth Fibonacci number using a formula.
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
(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