Last active
January 2, 2016 02:59
-
-
Save ronaldronson/8240452 to your computer and use it in GitHub Desktop.
Fibonacci function
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
/** ES 5 */ | |
function f (n) { return !!~[1,2].indexOf(n) ? 1 : f(n-1) + f(n-2) } | |
/** ES 6 */ | |
const f = n => !!~[1,2].indexOf(n) ? 1 : f(n-1) + f(n-2) | |
/** more crazy */ | |
const f = n => !!({1:1, 2:1})[n] ? 1 : f(n-1) + f(n-2) | |
/** | |
* Optimized function | |
*/ | |
function fib2(n) { | |
var arr = Array.apply(null, {length: n + 1}) | |
.map(function(val, i) { return i }); | |
for (var i = 2; i <= n; i++) { | |
arr[i] = arr[i - 1] + arr[i - 2]; | |
} | |
return arr[n]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment