Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://www.jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Fibonacci without loops nor recursions //JavaScript Function



[UPDATED CODE AND HELP CAN BE FOUND HERE]


usage

for(var i = 11; --i > -11; document.write(i, " = ", fibonacci(i), "<br />"));


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);
};
*/
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS