Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fibonacci Without Loops Nor Recursions //JavaScript Function

DZone's Guide to

Fibonacci Without Loops Nor Recursions //JavaScript Function

·
Free Resource


[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage


for(var i = 11; --i > -11; document.write(i, " = ", fibonacci(i), "
"));
code //+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/math/fibonacci [v1.0] fibonacci = function(n){ return Math.round(Math.pow((Math.sqrt(5) + 1) / 2, Math.abs(n)) / Math.sqrt(5)) * (n < 0 && n % 2 ? -1 : 1); }; /*this one requires very few recursions fibonacci = function(n){ var x; return n < 2 ? n : n % 2 ? (x = fibonacci(n = -(-n >> 1))) * x + (x = fibonacci(n - 1)) * x : (fibonacci(n >>= 1) + 2 * fibonacci(n - 1)) * fibonacci(n); }; */
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}