# gcd

Returns the greatest common divisor of the given integers.

Wikipedia:

$$\operatorname{gcd}(54,24)$$

The number 54 can be expressed as a product of two integers in several different ways:

$$54\times 1=27\times 2=18\times 3=9\times 6.$$

Thus the complete list of divisors of 54 is: $$1,2,3,6,9,18,27,54$$

Similarly, the divisors of 24 are: $$1,2,3,4,6,8,12,24$$

The numbers that these two lists have in common are the common divisors of 54 and 24, that is: $$1,2,3,6$$

Of these, the greatest common divisor is 6.

gcd(a,b)
Returns the greatest common divisor of the given integers.
COPY/// @func   gcd(a,b)
///
/// @desc   Returns the greatest common divisor of the given integers.
///
/// @param  {real}      a           positive integer
/// @param  {real}      b           positive integer
///
/// @return {real}      greatest common divisor
///

function gcd(a, b)
{
while (b != 0) {
var r = a mod b;
a = b;
b = r;
}
return abs(a);
}


Contributors: xot

GitHub: View · Commits · Blame · Raw