给定一个正整数N ,任务是将分数2 / N表示为形式为/ m的三个不同的正整数之和,即(2 / N)=(1 / x)+(1 / y)+( 1 / z)并打印x , y和z 。
例子:
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
方法:很容易推断出,对于N = 1 ,将没有解决方案。对于N> 1 , (2 / N)可以表示为(1 / N)+(1 / N) ,问题可以简化为用两个分数之和表示。现在,找到(1 / N)与1 /(N + 1)之间的差,并得到分数1 /(N *(N + 1)) 。因此,解为(2 / N)=(1 / N)+(1 /(N + 1))+(1 /(N *(N + 1)))其中x = N , y = N + 1并且z = N *(N +1) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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;
}
Java
// 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
Python3
# 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
C#
// 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
Javascript
输出:
5 6 30