Skip to content

Instantly share code, notes, and snippets.

@anmonteiro
Created July 14, 2014 10:11
Show Gist options
  • Save anmonteiro/2d896dce85f302f8517a to your computer and use it in GitHub Desktop.
Save anmonteiro/2d896dce85f302f8517a to your computer and use it in GitHub Desktop.
Caching Fibonacci (JavaScript)
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