📌  相关文章
📜  对,它们的五次幂之差为X

📅  最后修改于: 2021-05-05 01:17:42             🧑  作者: Mango

给定一个整数X ,任务是找到对AB ,使它们的五次幂之差为X,即A 5 – B 5 =X 。如果没有这样的配对,则打印“不可能”。

天真的方法:一个简单的解决方案是使用两个for循环,一个用于A,一个用于B,范围为-10 9到10 9
高效方法:想法是使用数学技术缩小A和B的范围

由于A 5 – B 5 = X => A 5 = X + B 5 。从不等式可以明显看出,要使A尽可能高,B也必须尽可能高。

通过二项式展开,我们知道

因此,可以说LHS的最大值为4N 4

由于A和B也可以为负,因此我们简单地推断范围,最终得到的范围是[-120,120]。

下面是上述方法的实现:

C++
// C++ implementation to find a pair
// of integers A & B such that
// difference of fifth power is
// equal to the given number X
 
#include 
using namespace std;
 
// Function to find a pair
// of integers A & B such that
// difference of fifth power is
// equal to the given number X
void findPair(int x)
{
    int lim = 120;
 
    // Loop to choose every possible
    // pair with in the range
    for (int i = -lim; i <= lim; i++) {
        for (int j = -lim; j <= lim; j++) {
 
            // Check if equation holds
            if (pow(i, 5) - pow(j, 5) == x) {
                cout << i << ' ' << j << endl;
                return;
            }
        }
    }
    cout << "-1";
}
 
// Driver Code
signed main()
{
    int X = 33;
 
    // Function Call
    findPair(X);
    return 0;
}


Java
// Java implementation to find a
// pair of integers A & B such
// that difference of fifth power
// is equal to the given number X
class GFG{
 
// Function to find a pair
// of integers A & B such that
// difference of fifth power is
// equal to the given number X
static void findPair(int x)
{
    int lim = 120;
 
    // Loop to choose every possible
    // pair with in the range
    for(int i = -lim; i <= lim; i++)
    {
       for(int j = -lim; j <= lim; j++)
       {
            
          // Check if equation holds
          if (Math.pow(i, 5) -
              Math.pow(j, 5) == x)
          {
              System.out.print(i + " " +
                               j + "\n");
              return;
          }
       }
    }
    System.out.print("-1");
}
 
// Driver Code
public static void main(String[] args)
{
    int X = 33;
 
    // Function Call
    findPair(X);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to find 
# a pair of integers A & B such 
# that difference of fifth power
# is equal to the given number X
import math
 
# Function to find a pair
# of integers A & B such that
# difference of fifth power is
# equal to the given number X
def findPair(x):
 
    lim = 120
 
    # Loop to choose every possible
    # pair with in the range
    for i in range(-lim, lim + 1):
        for j in range(-lim, lim + 1):
             
            # Check if equation holds
            if (math.pow(i, 5) -
                math.pow(j, 5) == x):
                print (i, end = ' ')
                print (j, end = '\n')
                return
     
    print ("-1")
 
# Driver Code
X = 33
 
# Function Call
findPair(X)
 
# This code is contributed by PratikBasu


C#
// C# implementation to find a
// pair of integers A & B such
// that difference of fifth power
// is equal to the given number X
using System;
 
class GFG{
 
// Function to find a pair of
// integers A & B such that
// difference of fifth power is
// equal to the given number X
static void findPair(int x)
{
    int lim = 120;
 
    // Loop to choose every possible
    // pair with in the range
    for(int i = -lim; i <= lim; i++)
    {
       for(int j = -lim; j <= lim; j++)
       {
           
          // Check if equation holds
          if (Math.Pow(i, 5) -
              Math.Pow(j, 5) == x)
          {
              Console.Write(i + " " +
                            j + "\n");
              return;
          }
       }
    }
    Console.Write("-1");
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 33;
 
    // Function call
    findPair(X);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1 -2

时间复杂度: O(240 * 240)