02 Euclid's Algorithm for GCD
Eculid's Formla for GCD:
Iterative Method
int GCD(int a, int b) {
while((a % b) > 0) {
int temp = a;
a = b;
b = temp % b;
}
return b;
}
Recursive Method
int gcd(int a, int b)
{
// BASE CASE
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a % b, b);
return gcd(a, b % a);
}
Question 1
Question 2
Last updated