Created
May 26, 2013 19:35
-
-
Save jremmen/5653783 to your computer and use it in GitHub Desktop.
js: naive fib
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
// naive fibs with memoization | |
function fib(n, acc) { | |
if(n >= 1) { | |
if(acc[n]) { | |
return acc[n]; | |
} else { | |
acc[n] = fib(n - 1, acc) + fib(n - 2, acc); | |
return acc[n]; | |
} | |
} else { | |
return 1; | |
} | |
} | |
function fib(n, acc) { | |
return | |
n >= 1 ? acc[n] ? acc[n] : acc[n] = fib(n - 1, acc) + fib(n - 2, acc) : 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment