Skip to content

Instantly share code, notes, and snippets.

@ungarson
Last active June 12, 2019 05:19
Show Gist options
  • Save ungarson/fe9c07b81b2228da0bcb7f2f9c492bca to your computer and use it in GitHub Desktop.
Save ungarson/fe9c07b81b2228da0bcb7f2f9c492bca to your computer and use it in GitHub Desktop.
Fibonacci with memoization
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