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, 20) = 60
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.
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Wedderburn–Etherington number
- Modular Exponentiation of Complex Numbers
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Program for sum of cosh(x) series upto Nth term
- Find prime numbers in the first half and second half of an array
- Euclid–Mullin Sequence
- Count of elements that can be deleted without disturbing the mean of the initial array
- Count non-negative triplets with sum equal to N
- Find maximum xor of k elements in an array
- Find the prime P using given four integers