Skip to content

Instantly share code, notes, and snippets.

@aershov24
Created October 13, 2020 05:28
Show Gist options
  • Select an option

  • Save aershov24/8a4c90bb54e79d8c36921d7e76b7d35d to your computer and use it in GitHub Desktop.

Select an option

Save aershov24/8a4c90bb54e79d8c36921d7e76b7d35d to your computer and use it in GitHub Desktop.
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
// Iterative Solution for Fibonacci Numbers
function *fibonacci(n) {
const infinite = !n && n !== 0;
let current = 0;
let next = 1;
while (infinite || n--) {
yield current;
[current, next] = [next, current + next];
}
}
// Recursive Solution for Fibonacci Numbers
function *fibonacci(n = null, current = 0, next = 1) {
if (n === 0) {
return current;
}
let m = n !== null ? n - 1 : null;
yield current;
yield *fibonacci(m, next, current + next);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment