greatestCommonDivisorOfMany function

int greatestCommonDivisorOfMany (List<int> integers)

Returns the greatest common divisor (gcd) of two integers using Euclid's algorithm.

Implementation

int greatestCommonDivisorOfMany(List<int> integers) {
  if (null == integers) {
    throw ArgumentError.notNull('integers');
  }

  if (integers.length == 0) {
    return 0;
  }

  var gcd = integers[0].abs();

  for (var i = 1; (i < integers.length) && (gcd > 1); i++) {
    gcd = greatestCommonDivisor(gcd, integers[i]);
  }

  return gcd;
}