Answers and Comments


User Avatar
Written by CodeGuru


O(n) solution:


int gcd(int a, int b)
{
int c = min(a, b);
while (a % c + b % c) --c;
return c;
}


User Avatar
Written by CMaster


Euclidean algorithm would give O(log(n)) solution.

http://en.wikipedia.org/wiki/Euclidean_algorithm


Saved Stories

Sponsored Categories