greatestCommonDivisorOfMany function

int greatestCommonDivisorOfMany(
  1. List<int> integers
)

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

Implementation

int greatestCommonDivisorOfMany(List<int> 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;
}