Created
November 22, 2016 19:55
-
-
Save andrewbonnington/5111855730389ac119a70e2cfd3578fd to your computer and use it in GitHub Desktop.
Memoized fibonacci calculation
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 fibonacci(n) { | |
| if (n == 0) return 0; | |
| if (n == 1) return 1; | |
| if (fibonacci.cache[n] > 0) return fibonacci.cache[n]; | |
| return fibonacci.cache[n] = fibonacci(n - 1) + fibonacci(n - 2); | |
| } | |
| fibonacci.cache = []; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment