gcd.c
/*
Greatest Common Denominator Functions
*/

// Single GCD - Euclid's Algorithm

int gcd(int x, int y)
{
	int g;
	
	if(x < 0) x = -x;
	if(y < 0) y = -y;

	if((x+y) == 0) return 0;

	g = y;
	while(x > 0)
	{
		g = x;
		x = y % x;
		y = g;
	}

	return g;
}


// gcd of array of numbers

int array_gcd(int m, int *x)
{
	int i, g;

	if(m < 1)
		return 0;

	g = x[0];
	for(i =1; i < m; ++i)
	{
		g = gcd(g, x[i]);

		// optimization, random x[i], g == 1 60% of the time 
		if(g == 1)
			return 1;
	}
	return g;
}