Created
April 13, 2015 17:43
-
-
Save pflannery/d2906ac76e5a63f66a66 to your computer and use it in GitHub Desktop.
Memoize Fibonacci
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
function() { | |
var fibMemo = [0, 1]; | |
function fib(n) { | |
var f; | |
for (var k = fibMemo.length; k <= n; k++) { | |
f = fibMemo[k - 1] + fibMemo[k - 2]; | |
fibMemo[k] = f; | |
} | |
return f || fibMemo[n]; | |
} | |
for(var index=0; index<10; index++) | |
console.log("index", fib(index)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment