Skip to content

Instantly share code, notes, and snippets.

@mindoftea
Created May 10, 2014 06:08
Show Gist options
  • Save mindoftea/f12d37569c378f316326 to your computer and use it in GitHub Desktop.
Save mindoftea/f12d37569c378f316326 to your computer and use it in GitHub Desktop.
A recursive, caching JavaScript implementation of the famous Fibonacci function. Each time you call it, it gets faster.
var fibonacci=function()
{
var x=[0,1];
return function(n)
{
if(x[n]!==undefined)
{
return x[n];
}
else
{
x[n]=fibonacci(n-1)+fibonacci(n-2);
return x[n];
}
}
};
fibonacci=fibonacci();
print( fibonacci(7) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment