Files
30-seconds-of-code/snippets/gcdOfArray.md
Rohit Tanwar d624fe5843 Add gcdOfArray
2017-12-20 17:58:36 +05:30

307 B

gcdOfArray

It finds the GCD of all the numbers in an array by using array.reduce and the fact that gcd(a,b,c) = gcd(gcd(a,b),c)

const gcdOfArray = arr =>
  {
  const gcd = (x, y) => !y ? x : gcd(y, x % y);
  arr.reduce((a,b) => gcd(a,b))
  }
// functionName(sampleInput) -> sampleOutput