Skip to content

Instantly share code, notes, and snippets.

@Prinzhorn
Forked from 140bytes/LICENSE.txt
Created October 3, 2011 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Prinzhorn/1258622 to your computer and use it in GitHub Desktop.
Save Prinzhorn/1258622 to your computer and use it in GitHub Desktop.
Fibonacci with dynamic programming
/**
@param n calculate fibonacci number of n
*/
function f(a){
return a < 2 ? //Simple case for first two fibonacci numbers
a :
f[a]||(f[a]=f(a-1)+f(a-2)) //Return value of cache or add value to cache if needed. The function is used as cache.
}
function f(a){return a<2?a:f[a]||(f[a]=f(a-1)+f(a-2))}
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.
{
"name": "fastRecursiveFibonacciWithDynamicProgramming",
"description": "A recursive but fast implementation of Fibonacci using dynamic programming.",
"keywords": [
"fibonacci",
"recursive",
"fast",
"dynamic",
"programming"
]
}
<!DOCTYPE html>
<title>Fast recursive Fibonacci</title>
<div>Expected value: <b>190392490709135</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function f(a){return a<2?a:f[a]||(f[a]=f(a-1)+f(a-2))}
document.getElementById( "ret" ).innerHTML = myFunction(70);
</script>
@tsaniel
Copy link

tsaniel commented Oct 3, 2011

Try to reuse the original function
function f(n,c){return(c=c||[0,1,1])[n]||(c[n]=f(n-1,c)+f(n-2,c))}

@Prinzhorn
Copy link
Author

But then it's leaking the global scope, which is against rule 3. But I see others doing that.

@tsaniel
Copy link

tsaniel commented Oct 3, 2011

Em... It wouldn't leak the global scope actually.
For example, the following code will cause "Uncaught ReferenceError: f is not defined",
as Function expressions is not Function declarations.

var a = function f(){};
alert(f);

http://kangax.github.com/nfe/

@Prinzhorn
Copy link
Author

OK, I see what you did there.

@jed
Copy link

jed commented Oct 3, 2011

why not use the function as the cache?

function f(a){return f[a]=a<3?1:f[a]||f(a-1)+f(a-2)}

@fgnass
Copy link

fgnass commented Oct 3, 2011

Smart move!

@Prinzhorn
Copy link
Author

That was indeed a smart move. But it destroyed the case where a is 0. Your code returns 1 for f(0) which is by definition 0.
I committed a tweaked version of yours, which is just two bytes longer.

@jed
Copy link

jed commented Oct 3, 2011

well, you only need the extra two bytes avoid the unneeded assignment. since it's so cheap, i think it's worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment