Skip to content

Instantly share code, notes, and snippets.

@ronaldronson
Last active January 2, 2016 02:59
Show Gist options
  • Save ronaldronson/8240452 to your computer and use it in GitHub Desktop.
Save ronaldronson/8240452 to your computer and use it in GitHub Desktop.
Fibonacci function
/** 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