Created
July 14, 2014 10:11
-
-
Save anmonteiro/2d896dce85f302f8517a to your computer and use it in GitHub Desktop.
Caching Fibonacci (JavaScript)
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 fib = (function() { | |
var fibArr = []; | |
var calcFib = function( n ) { | |
return ( n <= 1 ? 1 : (fib( n - 1 ) + fib( n - 2 ))); | |
}; | |
return function( n ) { | |
if( !fibArr[ n ] ) { | |
fibArr[ n ] = calcFib( n ); | |
} | |
return fibArr[ n ]; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment