Problem 233
GCD of an Array
Easy · 10 pts · Math, Number Theory
Problem
You are given an array of N positive integers. Print their greatest common divisor: the largest positive integer that divides every element exactly. The GCD of several numbers can be built up one at a time: g = GCD(g, A[i]) for each element, starting with g = A[0]. Each step uses Euclid's algorithm. Once g reaches 1, it cannot get any smaller, so you may stop early.
Input
First line: integer N. Second line: N positive integers.
Output
Print the GCD of the array.
Constraints
1 <= N <= 100000; 1 <= A[i] <= 1000000000
Example input
3 4 6 8
Example output
2
Explanation
The numbers 4, 6 and 8 are all even, so 2 divides all of them. No larger number does (3 does not divide 4, and 4 does not divide 6), so the GCD is 2.