Skip to main content

Euclidean GCD

In mathematics, the Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two integers (numbers), the largest number that divides them both without a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in his Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a calculation according to well-defined rules, and is one of the oldest algorithms in common use. It can be used to reduce fractions to their simplest form, and is a part of many other number-theoretic and cryptographic calculations.


function euclidean(a,b)
{
  //Logic
  if(a === 0)
  {
    x = 0;
    y = 1;
    var data = [b,x,y];
    return data;
  }
  var gcd = euclidean(b % a, a);
  gcd[1] = gcd[2] - (b / a) * gcd[1];
  gcd[2] = gcd[1];
  return gcd;
}