I know that Euclid's algorithm is the best algorithm for get the GCD (great common divisor) for a list the positive integer numbers. But, in the practice, you can write two codes por evaluate the gcd (for my case, i decided use java, but c/c++ may be another option).
I need to get the most efficient code of two possibilities form to programming.
Recursive Mode, you can write...
static long gcd(long a, long b){
a = Math.abs(a); b = Math.abs(b);
return (a==0)?b:gcd(b, a%b);
}
And, iterative mode, looks like ...
static long gcd(long a, long b) {
long r, i;
while(b!=0){
r = a % b;
a = b;
b = r;
}
return a;
}
Regards,
UPDATE
We can do that with the Binary GCD, and the easy code is like that
int gcd(int a, int b)
{
while(b) b ^= a ^= b ^= a %= b;
return a;
}
Great Discussion.