Skip to content

Instantly share code, notes, and snippets.

@thiskevinwang
Last active January 24, 2019 02:46
Show Gist options
  • Select an option

  • Save thiskevinwang/cffbebe36f52e49c696c4750da60ff84 to your computer and use it in GitHub Desktop.

Select an option

Save thiskevinwang/cffbebe36f52e49c696c4750da60ff84 to your computer and use it in GitHub Desktop.
Fibonacci Function with Memoization (node.js)
console.log(process.argv);
function fibonacci(n, cache) {
cache = cache || {};
if (cache[n]) { return cache[n] };
if (n <= 2) { return 1 };
return cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache);
}
console.log(fibonacci(process.argv[2], null));
// $ node fibonacciMemo.js 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment