Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)

Given a positive integer N, the task is to represent the fraction 2 / N as the sum of three distinct positive integers of the form 1 / m i.e. (2 / N) = (1 / x) + (1 / y) + (1 / z) and print x, y and z.

Examples:

Input: N = 3
Output: 3 4 12
(1 / 3) + (1 / 4) + (1 / 12) = ((4 + 3 + 1) / 12)
= (8 / 12) = (2 / 3) i.e. 2 / N



Input: N = 28
Output: 28 29 812

Approach: It can be easily inferred that for N = 1, there will be no solution. For N > 1, (2 / N) can be represented as (1 / N) + (1 / N) and the problem gets reduced to representing it as a sum of two fractions. Now, find the difference between (1 / N) and 1 / (N + 1) and get the fraction 1 / (N * (N + 1)). Therefore, the solution is (2 / N) = (1 / N) + (1 / (N + 1)) + (1 / (N * (N + 1))) where x = N, y = N + 1 and z = N * (N + 1).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the required fractions
void find_numbers(int N)
{
    // Base condition
    if (N == 1) {
        cout << -1;
    }
  
    // For N > 1
    else {
        cout << N << " " << N + 1 << " "
             << N * (N + 1);
    }
}
  
// Driver code
int main()
{
    int N = 5;
  
    find_numbers(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to find the required fractions
static void find_numbers(int N)
{
    // Base condition
    if (N == 1
    {
        System.out.print(-1);
    }
  
    // For N > 1
    else 
    {
        System.out.print(N + " " + (N + 1) + 
                             " " + (N * (N + 1)));
    }
}
  
// Driver code
public static void main(String []args) 
{
    int N = 5;
  
    find_numbers(N);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to find the required fractions 
def find_numbers(N) :
  
    # Base condition 
    if (N == 1) :
        print(-1, end = ""); 
  
    # For N > 1 
    else :
        print(N, N + 1 , N * (N + 1)); 
  
# Driver code 
if __name__ == "__main__"
  
    N = 5
  
    find_numbers(N);
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System; 
  
class GFG 
{
  
// Function to find the required fractions
static void find_numbers(int N)
{
    // Base condition
    if (N == 1) 
    {
        Console.Write(-1);
    }
  
    // For N > 1
    else
    {
        Console.Write(N + " " + (N + 1) + 
                          " " + (N * (N + 1)));
    }
}
  
// Driver code
public static void Main(String []args) 
{
    int N = 5;
  
    find_numbers(N);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

5 6 30


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.