Greatest Common Divisor from a set of more than 2 integers

greatest common divisor algorithm
gcd
gcd calculator
gcd of n numbers
gcd of n numbers in python
javascript greatest common divisor
greatest common divisor - leetcode
greatest common divisor java

There are several questions on Stack Overflow discussing how to find the Greatest Common Divisor of two values. One good answer shows a neat recursive function to do this.

But how can I find the GCD of a set of more than 2 integers? I can't seem to find an example of this.


Can anyone suggest the most efficient code to implement this function?

static int GCD(int[] IntegerSet)
{
    // what goes here?
}

And here you have code example using LINQ and GCD method from question you linked. It is using theoretical algorithm described in other answers ... GCD(a, b, c) = GCD(GCD(a, b), c)

static int GCD(int[] numbers)
{
    return numbers.Aggregate(GCD);
}

static int GCD(int a, int b)
{
    return b == 0 ? a : GCD(b, a % b);
}

Undergraduate Mathematics/Greatest common divisor, (a, b), where a and b are not both zero, may be defined alternatively and equivalently as the smallest positive integer d which can be written in the form d = a·p + b·q where p and q are integers. There are several questions on Stack Overflow discussing how to find the Greatest Common Divisor of two values. One good answer shows a neat recursive function to do this.

You could use this common property of a GCD:

GCD(a, b, c) = GCD(a, GCD(b, c)) = GCD(GCD(a, b), c) = GCD(GCD(a, c), b)

Assuming you have GCD(a, b) already defined it is easy to generalize:

public class Program
{
    static void Main()
    {
        Console.WriteLine(GCD(new[] { 10, 15, 30, 45 }));
    }

    static int GCD(int a, int b)
    {
        return b == 0 ? a : GCD(b, a % b);
    }

    static int GCD(int[] integerSet)
    {
        return integerSet.Aggregate(GCD);
    }    
}

What is the greatest common divisor of 20 and 36?, What is the greatest common divisor of 20 and 36? By definition, the gcd of two or more integers, that are not all zero, is the largest positive integer that divides all these numbers. For example: For example: gcd(9, 15) = 3 gcd(9, 15, 21) = 3 gcd(0, 10) = 10

Here's the C# version.

  public static int Gcd(int[] x) {
      if (x.length < 2) {
          throw new ArgumentException("Do not use this method if there are less than two numbers.");
      }
      int tmp = Gcd(x[x.length - 1], x[x.length - 2]);
      for (int i = x.length - 3; i >= 0; i--) {
          if (x[i] < 0) {
              throw new ArgumentException("Cannot compute the least common multiple of several numbers where one, at least, is negative.");
          }
          tmp = Gcd(tmp, x[i]);
      }
      return tmp;
  }

  public static int Gcd(int x1, int x2) {
      if (x1 < 0 || x2 < 0) {
          throw new ArgumentException("Cannot compute the GCD if one integer is negative.");
      }
      int a, b, g, z;

      if (x1 > x2) {
          a = x1;
          b = x2;
      } else {
          a = x2;
          b = x1;
      }

      if (b == 0) return 0;

      g = b;
      while (g != 0) {
          z= a % g;
          a = g;
          g = z;
      }
      return a;
  }

}

Source http://www.java2s.com/Tutorial/Java/0120__Development/GreatestCommonDivisorGCDofpositiveintegernumbers.htm

Greatest Common Divisor from a set of more than 2 integers, And here you have code example using LINQ and GCD method from question you linked. It is using theoretical algorithm described in other answers GCD(a, b  The greatest common divisor (GCD), also called the greatest common factor, of two numbers is the largest number that divides them both. For instance, the greatest common factor of 20 and 15 is 5, since 5 divides both 20 and 15 and no larger number has this property. The concept is easily extended to sets of more than two numbers: the GCD of a set of numbers is the largest number dividing each of them.

Wikipedia:

The gcd is an associative function: gcd(a, gcd(b, c)) = gcd(gcd(a, b), c).

The gcd of three numbers can be computed as gcd(a, b, c) = gcd(gcd(a, b), c), or in some different way by applying commutativity and associativity. This can be extended to any number of numbers.

Just take the gcd of the first two elements, then calculate the gcd of the result and the third element, then calculate the gcd of the result and fourth element...

GCD of more than two (or array) numbers, Given an array of numbers, find GCD of the array elements. In a previous post we find GCD of two number. Examples: Input : arr[] = {1, 2, 3} Output : 1  The Greatest Common Divisor (GCD) of two whole numbers, also called the Greatest Common Factor (GCF) and the Highest Common Factor (HCF), is the largest whole number that's a divisor (factor) of both of them. For instance, the largest number that divides into both 20 and 16 is 4.

Rewriting this as a single function...

    static int GCD(params int[] numbers)
    {
        Func<int, int, int> gcd = null;
        gcd = (a, b) => (b == 0 ? a : gcd(b, a % b));
        return numbers.Aggregate(gcd);
    } 

GCD Calculator, Use this page to calculate the gcd of a set of two or more numbers. The GCD calculator allows you to quickly find the greatest common divisor of a set of  Write a JavaScript function to get the greatest common divisor (gcd) of two integers. Note: According to Wikipedia - In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

Greatest Common Divisors, you see that 2 is the only integer bigger than 1 which divides both 4 and 6. If one of the numbers is 0, the other is the greatest common divisor of the pair. common divisor of more than two divisors, just compute the greatest common divisor  The greatest common divisor (also known as greatest common factor, highest common divisor or highest common factor) of a set of numbers is the largest positive integer number that devides all the numbers in the set without remainder. It is the biggest multiple of all numbers in the set. The GCD is most often calculated for two numbers, when it is used to reduce fractions to their lowest terms.

GCD Calculator - Greatest Common Divisor, GCD of 2 or more numbers Calculator; List of Divisors; What is the GCD? (​Definition); How to How to demonstrate that if GCD(b,c)=1, then GCD(a,b*c) = GCD(a,b).GCD(a,c); How to By convention, only the positive value is given. PG​CD(a  Python Basic: Exercise-31 with Solution. Write a Python program to compute the greatest common divisor (GCD) of two positive integers. Pictorial Presentation:

Greatest Common Divisor, The concept is easily extended to sets of more than two numbers: the GCD of a set of numbers is the largest number dividing each of them. The GCD is used for​  GCD (12,12) =12 144=2^4*3^2 ; (2*2*3) & (2*2*3) groups will intuitively yield gcd =12 which is greatest. Therefore Greatest possible common Divisor is 12 □ANSWER. Correction :- While solving the Question, I have, by mistake, taken the product of two integers should be less than 145 instead of two integers should be

Comments
  • GCD is associative and commutative like + and *, so you can successively apply GCD to the numbers in any order.
  • if C# has the "inject" or "reduce" method for lists, like many functional languages do, then this is a snap.
  • A minor upspeed: return b == 0 ? a : GCD(Math.Min(a,b), Math.Max(a,b) % Math.Min(a,b)); This saves up to 50% of the % divisions when numbers[] contains ascending values.
  • @robert4 - Your solution gets divide by zero errors. Need to add a check for a == 0 as well. return b == 0 ? a : (a == 0 ? b : GCD(Math.Min(a,b), Math.Max(a,b) % Math.Min(a,b)));
  • @NightOwl888 You have a point. return (a == 0 || b == 0) ? a|b : GCD(Math.Min(a, b), Math.Max(a, b) % Math.Min(a, b));
  • Thanks, exactly what I need, but your edit came slightly too late, Matajon came up with the same answer just before you... So I think it's only fair for me to accept his. (+1 anyway)
  • I was just about to mention that you'd missed out the second function... but you fixed it :) Thanks, this is exactly what I need.
  • I was about to accept your answer, but Darin Dimitrov and Matajon have come up with a neater method. Sorry! (+1 anyway)
  • Sorry about that. I was too hasty thinking I'm the only one with the right answer. ;) If speed is important, this method should be tested against the LINQ methods described by Darin and Matajon. If not, I'm more than happy to say that you should use the LINQ method as it is much more elegant.
  • please, add some explanation to your answer.
  • Sure it could take some more explanations, but it's the only answer breaking on gcd = 1, so kudos.