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 email@example.com. 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.
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Find Kth smallest value for b such that a + b = a | b
- Number of pairs of lines having integer intersection points
- Percentage change in Hemisphere volume if radius is changed
- Program to find the time remaining for the day to complete
- Find two numbers with the given LCM and minimum possible difference
- Find an integer that is common in the maximum number of given arithmetic progressions
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Height of Pyramid formed with given Rectangular Box
- Make all the array elements odd with minimum operations of given type
- Divide N into K unique parts such that gcd of those parts is maximum