GCD: Greatest Common Divisor LCD: Least Common Multiple
The formula is:
The problems involving GCD and LCM is usually solved with the above equation. Generally, we find the GCD first and then we get LCM with the equation. Theres two way we can find the GCD:
- Naive Method: if , divide by and until 1. And the number with which it is divisible without reminder is the GCD.
- Euclidean Method: if , and then, continue this process until . And is the GCD of
Here is a recursive code of the GCD
:
Euclidean method is very efficient for long long Datatype
it takes about 100~150 iterations to find the GCD.
We can find the LCM from the GCD with the above equation.
But how do we find the GCD or LCM of more than two numbers? Two numbers at at time.