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

Greatest Common Divisor //JavaScript Function

DZone's Guide to

Greatest Common Divisor //JavaScript Function

·
Free Resource


[UPDATED CODE AND HELP CAN BE FOUND HERE]


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;
}
Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}