# GCD, LCM and Distributive Property

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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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.

```// 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;
}
```

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)

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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.7 Average Difficulty : 3.7/5.0
Based on 12 vote(s)