Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z
Given n, find x, y, z such that x, y, z satisfy the equation “2/n = 1/x + 1/y + 1/z”
There are multiple x, y and z that satisfy the equation print anyone of them, if not possible then print -1.
Input : 3 Output : 3 4 12 Explanation: here 3 4 and 12 satisfy the given equation Input : 7 Output : 7 8 56
Note that for n = 1 there is no solution, and for n > 1 there is solution x = n, y = n+1, z = n·(n+1).
To come to this solution, represent 2/n as 1/n+1/n and reduce the problem to represent 1/n as a sum of two fractions. Let’s find the difference between 1/n and 1/(n+1) and get a fraction 1/(n*(n+1)), so the solution is
2/n = 1/n + 1/(n+1) + 1/(n*(n+1))
x is 7 y is 8 z is 56
Time complexity: O(1)
We can write 2/n = 1/n + 1/n. And further as 2/n = 1/n + 1/2n + 1/2n.
Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.