Created
January 6, 2017 12:24
-
-
Save Teqqles/7477104ee159d63f4becc0cfb5aaa0f9 to your computer and use it in GitHub Desktop.
Cached Javascript Fibonacci numbers
This file contains 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
var fibCache = [0, 1]; | |
var yourself = { | |
fibonacci : function(n) { | |
if (n <= 1) { | |
return n; | |
} | |
if (!fibCache[n]) { | |
fibCache[n] = this.fibonacci(n - 1) + | |
this.fibonacci(n - 2); | |
} | |
return fibCache[n]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment