Created
August 2, 2016 23:05
-
-
Save antenando/22baeda5725a7657b986dd21014c710d to your computer and use it in GitHub Desktop.
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
// recursive | |
/* | |
function fibonacci(n) { | |
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); | |
} | |
*/ | |
// cached | |
function fibonacci(n, cache) { | |
cache = cache || {} | |
if (cache[n]) return cache[n]; | |
if (n < 2) return n; | |
return cache[n] = fibonacci( n - 1, cache ) + fibonacci( n - 2, cache); | |
} | |
for (var i = 0; i <= 100; i++) { | |
console.log('Fibonacci of %s is %s', i, fibonacci(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment