Created
May 10, 2014 06:08
-
-
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.
This file contains hidden or 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 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