Common Divisor of Array

Published by Mateusz Mędrowski in

Write a function that returns the greatest common divisor of all array elements. If the greatest common divisor is 1, return 1.

Examples

GCD([10, 20, 40]) ➞ 10

GCD([1, 2, 3, 100]) ➞ 1

GCD([1024, 192, 2048, 512]) ➞ 64

Notes

  • Array elements are always greater than 0.
  • There is a minimum of two array elements given.
  • If you have any problems, check the Resources.
Watch a quick demo on how Edabit works.