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.

Examples:



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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find x y z that
// satisfies 2/n = 1/x + 1/y + 1/z...
#include <bits/stdc++.h>
using namespace std;
  
// function to find x y and z that
// satisfy given equation.
void printXYZ(int n)
{
    if (n == 1)
        cout << -1;
  
    else
        cout << "x is " << n << "\ny is "
              << n + 1 << "\nz is "
              << n * (n + 1);
}
  
// driver program to test the above function
int main()
{
    int n = 7;
    printXYZ(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find x y z that
// satisfies 2/n = 1/x + 1/y + 1/z...
import java.io.*;
  
class Sums {
    // function to find x y and z that
    // satisfy given equation.
    static void printXYZ(int n){
        if (n == 1)
            System.out.println(-1);
        else{
        System.out.println("x is "+ n);
        System.out.println("y is "+ (n+1));
        System.out.println("z is "+ (n * (n + 1)));
        }
    }
    // Driver program to test the above function
    public static void main (String[] args) {
        int n = 7;
        printXYZ(n);
    }
}
  
// This code is contributed by Chinmoy Lenka

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find x y z that
# satisfies 2/n = 1/x + 1/y + 1/z...
  
# function to find x y and z that
# satisfy given equation.
def printXYZ( n ):
    if n == 1:
        print(-1)
    else:
        print("x is " , n )
        print("y is " ,n + 1)
        print("z is " ,n * (n + 1))
  
# driver code to test the above function
n = 7
printXYZ(n)
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find x y z that
// satisfies 2/n = 1/x + 1/y + 1/z...
using System;
  
class GFG 
{
    // function to find x y and z that
    // satisfy given equation.
    static void printXYZ(int n)
    {
        if (n == 1)
            Console.WriteLine(-1);
        else
        {
            Console.WriteLine("x is "+ n);
            Console.WriteLine("y is "+ (n+1));
            Console.WriteLine("z is "+ (n * (n + 1)));
        }
    }
      
    // Driver program 
    public static void Main () 
    {
        int n = 7;
        printXYZ(n);
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find x y z that
// satisfies 2/n = 1/x + 1/y + 1/z...
  
// function to find x y and z that
// satisfy given equation.
function printXYZ($n)
{
    if ($n == 1)
        echo -1;
  
    else
        echo "x is " , $n , "\ny is "
                , $n + 1 , "\nz is ",
                     $n * ($n + 1);
}
  
    // Driver Code
    $n = 7;
    printXYZ($n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output:

x is 7
y is 8
z is 56

Time complexity: O(1)

Alternate Solution
We can write 2/n = 1/n + 1/n. And further as 2/n = 1/n + 1/2n + 1/2n.



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m