Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Last active August 3, 2020 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gladchinda/3a6d740c46584ad3c1b6f251244c6efd to your computer and use it in GitHub Desktop.
Save gladchinda/3a6d740c46584ad3c1b6f251244c6efd to your computer and use it in GitHub Desktop.
// 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;
}
}
}
@shalvah
Copy link

shalvah commented Aug 3, 2020

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment