Greatest GCD between some numbers

user182513 picture user182513 · Sep 8, 2010 · Viewed 13.8k times · Source

We've got some nonnegative numbers. We want to find the pair with maximum gcd. actually this maximum is more important than the pair! For example if we have:

2 4 5 15

gcd(2,4)=2

gcd(2,5)=1

gcd(2,15)=1

gcd(4,5)=1

gcd(4,15)=1

gcd(5,15)=5

The answer is 5.

Answer

Garee picture Garee · Sep 8, 2010

You can use the Euclidean Algorithm to find the GCD of two numbers.

while (b != 0) 
{
    int m = a % b;
    a = b;
    b = m;
}
return a;