Skip to content

Instantly share code, notes, and snippets.

@jonrandy
Created August 10, 2023 02:39
Show Gist options
  • Save jonrandy/68b248a328765003d8627f5243b32d0c to your computer and use it in GitHub Desktop.
Save jonrandy/68b248a328765003d8627f5243b32d0c to your computer and use it in GitHub Desktop.
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