
Input: 'X' = 2, ‘Y’=3
Output: "6"
As “6” is the smallest number that is divisible by both 2 and 3.
The first line will contain the integer 'T', denoting the number of test cases.
Each test case contains two space-separated integers ‘X’ and ‘Y’
For each test case, print the LCM of given integers ‘X’ and ‘Y’.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= 'T' <= 1000
1 <= ‘X’ , ‘Y’ <= 10^9
Time Limit: 1 sec
We will remove the common factors from both of them, which means that we will remove the GCD ( Greatest Common Divisor ) by dividing them by their gcd. Then they will become the co-prime numbers.
And LCM of two co-prime numbers is their multiplication. Then we will multiple again with gcd to get the final LCM.
So LCM (X, Y) = (X/GCD(X,Y) * (Y/GCD(X,Y) * GCD(X,Y).
On simplify LCM(X,Y) = X * Y/GCD(X,Y).
We can calculate the GCD of two numbers with the ‘ Euclidean algorithm ’, which says that:
GCD(A, B) = A if B == 0, else GCD(A, B) = GCD(B, A%B).