DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Greatest Common Divisor //JavaScript Function

11.04.2005
| 3670 views |
  • submit to reddit
        <a href="http://jsfromhell.com/math/mdc">

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

usage

alert(mdc([8, 4, 12]));

code

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/mdc [v1.0]

mdc = function(o){
	if(!o.length)
		return 0;
	for(var r, a, i = o.length - 1, b = o[i]; i;)
		for(a = o[--i]; r = a % b; a = b, b = r);
	return b;
}