A 140byt.es fibonacci number calculation snippet inspired by the other Fibonacci implementations (https://gist.github.com/998671 , https://gist.github.com/979716 , ) and the trick mentioned here.
-
-
Save boast/1723596 to your computer and use it in GitHub Desktop.
Recursive Fibonacci with Caching
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 f(n){ | |
return f[n] = // Assign and return the cached n-th fibonacci number. Were using the function itself to cache the numbers - cool uh? | |
2>n ? // If we're looking for 0 or 1 ... | |
n : // ... just return it, else ... | |
f[n] || // ... look if we allready have the number in the cache or ... | |
f(n-1) + f(n-2) // ... calculate it recursively. | |
} |
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 f(n){return f[n]=2>n?n:f[n]||f(n-1)+f(n-2)} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "fibonacciWithCache", | |
"description": "Recursive Fibonacci number calculation with caching.", | |
"keywords": [ | |
"number", | |
"cache", | |
"recursive", | |
] | |
} |
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
<!DOCTYPE html> | |
<title>Fibonacci Test</title> | |
<div>Expected value: <b>190392490709135</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var myFunction = function f(n){return f[n]=2>n?n:f[n]||f(n-1)+f(n-2)} | |
document.getElementById("ret").innerHTML = myFunction(70) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no need to use closure.
So just
function f(n){return f[n]=2>n?n:f[n]||f(n-1)+f(n-2)}
is ok.