Skip to content

Instantly share code, notes, and snippets.

@sgarcia-dev
Last active January 22, 2019 03:17
Show Gist options
  • Save sgarcia-dev/0f71585ab0398561108ce3df09a27989 to your computer and use it in GitHub Desktop.
Save sgarcia-dev/0f71585ab0398561108ce3df09a27989 to your computer and use it in GitHub Desktop.
cache = {};
function cachedFibonacci(num) {
if (num <= 1) {
cache[num] = 1;
return 1;
} else if (cache[num]) {
return cache[num];
}
const result = cachedFibonacci(num - 1) + cachedFibonacci(num - 2);
cache[num] = result;
return result;
}
function fibonacciSequence(max, seq = [], current = 0) {
if (current <= max) {
seq.push(cachedFibonacci(current));
return fibonacciSequence(max, seq, current + 1);
} else {
return seq;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment