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
gcd(a1,a2,...,an)=gcd(a1,gcd(a2,gcd(a3...(gcd(a(n-1),an)))))
, so I would do it just step by step aborting if some gcd
evaluates to 1.
If your array is sorted, it might be faster to evaluate gcd
for small numbers earlier, since then it might be more likely that one gcd
evaluates to 1 and you can stop.