Created
August 10, 2023 02:39
-
-
Save jonrandy/68b248a328765003d8627f5243b32d0c to your computer and use it in GitHub Desktop.
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
function fibonacci(n) { | |
if (n < 0) throw RangeError("Negative arguments not implemented") | |
return fib(n)[0] | |
} | |
function fib(n) { | |
if (n) { | |
const [a, b] = fib(~~(n / 2)), | |
c = a * (b * 2n - a), | |
d = a * a + b * b | |
return n % 2 ? [d, c+d] : [c,d] | |
} | |
return [0n, 1n] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment