Last active
January 22, 2019 03:17
-
-
Save sgarcia-dev/0f71585ab0398561108ce3df09a27989 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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