Given three integers x, y, z, the task is to compute the value of GCD(LCM(x,y), LCM(x,z)).
Where, GCD = Greatest Common Divisor, LCM = Least Common Multiple
Examples:
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.
C++
// C++ program to compute value of GCD(LCM(x,y), LCM(x,z)) #include<bits/stdc++.h> using namespace std; // Returns value of GCD(LCM(x,y), LCM(x,z)) int findValue(int x, int y, int z) { int g = __gcd(y, z); // Return LCM(x, GCD(y, z)) return (x*g)/__gcd(x, g); } int main() { int x = 30, y = 40, z = 400; cout << findValue(x, y, z); return 0; } |
Java
// Java program to compute value // of GCD(LCM(x,y), LCM(x,z)) class GFG { // Recursive function to // return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Returns value of GCD(LCM(x,y), LCM(x,z)) static int findValue(int x, int y, int z) { int g = __gcd(y, z); // Return LCM(x, GCD(y, z)) return (x*g) / __gcd(x, g); } // Driver code public static void main (String[] args) { int x = 30, y = 40, z = 400; System.out.print(findValue(x, y, z)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to compute # value of GCD(LCM(x,y), LCM(x,z)) # Recursive function to # return gcd of a and b def __gcd(a,b): # Everything divides 0 if (a == 0 or b == 0): return 0 # base case if (a == b): return a # a is greater if (a > b): return __gcd(a-b, b) return __gcd(a, b-a) # Returns value of # GCD(LCM(x,y), LCM(x,z)) def findValue(x, y, z): g = __gcd(y, z) # Return LCM(x, GCD(y, z)) return (x*g)/__gcd(x, g) # driver code x = 30y = 40z = 400print("%d"%findValue(x, y, z)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to compute value // of GCD(LCM(x,y), LCM(x,z)) using System; class GFG { // Recursive function to // return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Returns value of GCD(LCM(x,y), // LCM(x,z)) static int findValue(int x, int y, int z) { int g = __gcd(y, z); // Return LCM(x, GCD(y, z)) return (x*g) / __gcd(x, g); } // Driver code public static void Main () { int x = 30, y = 40, z = 400; Console.Write(findValue(x, y, z)); } } // This code is contributed by // Smitha Dinesh Semwal. |
PHP
<?php // PHP program to compute value // of GCD(LCM(x,y), LCM(x,z)) // Recursive function to // return gcd of a and b function __gcd( $a, $b) { // Everything divides 0 if ($a == 0 or $b == 0) return 0; // base case if ($a == $b) return $a; // a is greater if ($a > $b) return __gcd($a - $b, $b); return __gcd($a, $b - $a); } // Returns value of GCD(LCM(x,y), // LCM(x,z)) function findValue($x, $y, $z) { $g = __gcd($y, $z); // Return LCM(x, GCD(y, z)) return ($x * $g)/__gcd($x, $g); } // Driver Code $x = 30; $y = 40; $z = 400; echo findValue($x, $y, $z); // This code is contributed by anuj_67. ?> |
Output:
120
As a side note, vice versa is also true, i.e., gcd(x, lcm(y, z)) = lcm(gcd(x, y), gcd(x, z)
Reference:
https://en.wikipedia.org/wiki/Distributive_property#Other_examples
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 contribute@geeksforgeeks.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.
Recommended Posts:
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- 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
- 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
- LCM of given array elements
- LCM of First n Natural Numbers
- Prime factors of LCM of array elements
- Maximum sum of distinct numbers with LCM as N
- Program to find LCM of two numbers

