Last active
June 12, 2019 05:19
-
-
Save ungarson/fe9c07b81b2228da0bcb7f2f9c492bca to your computer and use it in GitHub Desktop.
Fibonacci with memoization
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
function fib(pos, fibValues) { | |
if (typeof fibValues[pos] !== "undefined") { | |
return fibValues[pos]; | |
} | |
result = fib(pos - 1, fibValues) + fib(pos - 2, fibValues); | |
fibValues[pos] = result; | |
return result; | |
} | |
// Example of usage: | |
// let fibValues = [1, 1]; | |
// console.log(fib(30, fibValues)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment