Last active
January 24, 2019 02:46
-
-
Save thiskevinwang/cffbebe36f52e49c696c4750da60ff84 to your computer and use it in GitHub Desktop.
Fibonacci Function with Memoization (node.js)
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
| 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