Last active
August 3, 2020 19:01
-
-
Save gladchinda/3a6d740c46584ad3c1b6f251244c6efd 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
// A generator function for generating the first n number(s) | |
// of the Fibonacci sequence. | |
// Yields numbers as BigInt. | |
function* fibonacci(n) { | |
let i = 0; | |
n = (Number.isInteger(n) && n > 0) ? n : Infinity; | |
for (; i < 2 && i < n; i++) { | |
yield 1n; | |
} | |
if (n > 2) { | |
let [a, b] = [1n, 1n]; | |
for (; i < n; i++) { | |
[a, b] = [b, a + b]; | |
yield b; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!