Euclidean algorithm
The Euclidean Algorithm is a method for finding the greatest common divisor of two numbers. It operates on the principle that the GCD of two numbers remains the same even if the smaller number is subtracted from the larger number. Repeatedly subtract the smaller number from the larger number until one of them becomes 0.Once one of them becomes 0, the other number is the GCD of the original numbers.
Eg, n1 = 20, n2 = 15:
- gcd(20, 15) = gcd(20-15, 15) = gcd(5, 15)
- gcd(5, 15) = gcd(15-5, 5) = gcd(10, 5)
- gcd(10, 5) = gcd(10-5, 5) = gcd(5, 5)
- gcd(5, 5) = gcd(5-5, 5) = gcd(0, 5)
Hence, return 5 as the gcd.
// cpp code :- TC = O(min(a, b))
int findGcd(int a, int b) {
while(a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
if(a == 0) {
return b;
}
return a;