Input: x = 15, y = 20, z = 100 Output: 60 Input: x = 30, y = 40, z = 400 Output: 120
One way to solve it is by finding GCD(x, y), and using it we find LCM(x, y). Similarly, we find LCM(x, z) and then we finally find the GCD of the obtained results.
An efficient approach can be done by the fact that the following version of distributivity holds true:
GCD(LCM (x, y), LCM (x, z)) = LCM(x, GCD(y, z))
For example, GCD(LCM(3, 4), LCM(3, 10)) = LCM(3, GCD(4, 10)) = LCM(3, 2) = 6
This reduces our work to compute the given problem statement.
As a side note, vice versa is also true, i.e., gcd(x, lcm(y, z)) = lcm(gcd(x, y), gcd(x, z)
This article is contributed by Mazhar Imam Khan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Levelwise Alternating GCD and LCM of nodes in Segment Tree
- Find any pair with given GCD and LCM
- Minimum LCM and GCD possible among all possible sub-arrays
- Print any pair of integers with sum of GCD and LCM equals to N
- Count of all possible pairs having sum of LCM and GCD equal to N
- Finding LCM of more than two (or array) numbers without using GCD
- Program to find LCM of 2 numbers without using GCD
- Find the GCD of LCM of all unique pairs in an Array
- Find the ratio of LCM to GCD of a given Array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Smallest subsequence having GCD equal to GCD of given array
- LCM of factorial and its neighbors
- Find the other number when LCM and HCF given
- LCM and HCF of fractions
- Count the number of subsequences of length k having equal LCM and HCF
- Find two numbers with the given LCM and minimum possible difference
- Number of pairs such that their HCF and LCM is equal