📌  相关文章
📜  找出两个四次方之差等于N的数字

📅  最后修改于: 2021-04-29 17:49:20             🧑  作者: Mango

给定一个整数N ,任务是找到两个非负整数XY ,使得X 4 -Y 4 = N。如果不存在这样的对,则打印-1。
例子:

方法:
为了解决上述问题,我们必须观察到需要找到可能满足方程式的x和y的最小值和最大值

  • 由于XY为非负数,因此两个整数的最小值可以为0
  • XY的最大值可以是ceil(N (1/4) )
  • 因此,在[0,ceil(N (1/4) )]范围内进行迭代并找到满足条件的任意合适的XY对。

下面是上述方法的实现:

C++
// C++ implementation to find the
// values of x and y for the given
// equation with integer N
 
#include 
using namespace std;
 
// Function which find required x & y
void solve(int n)
{
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = ceil(pow(
        n, 1.0 / 4));
 
    for (int x = 0; x <= upper_limit; x++) {
 
        for (int y = 0; y <= upper_limit; y++) {
 
            // num1 stores x^4
            int num1 = x * x * x * x;
 
            // num2 stores y^4
            int num2 = y * y * y * y;
 
            // If condition is satisfied
            // the print and return
            if (num1 - num2 == n) {
                cout << "x = " << x
                     << ", y = " << y;
                return;
            }
        }
    }
 
    // If no such pair exists
    cout << -1 << endl;
}
 
// Driver code
int main()
{
    int n = 15;
 
    solve(n);
 
    return 0;
}


Java
// Java implementation to find the
// values of x and y for the given
// equation with integer N
import java.util.*;
 
class GFG{
 
// Function which find required x & y
static void solve(int n)
{
     
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = (int) (Math.ceil
                            (Math.pow(n, 1.0 / 4)));
 
    for(int x = 0; x <= upper_limit; x++)
    {
       for(int y = 0; y <= upper_limit; y++)
       {
           
          // num1 stores x^4
          int num1 = x * x * x * x;
           
          // num2 stores y^4
          int num2 = y * y * y * y;
           
          // If condition is satisfied
          // the print and return
          if (num1 - num2 == n)
          {
              System.out.print("x = " + x +
                             ", y = " + y);
              return;
          }
       }
    }
     
    // If no such pair exists
    System.out.print(-1);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 15;
 
    solve(n);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 implementation to find the
# values of x and y for the given
# equation with integer N
from math import pow, ceil
 
# Function which find required x & y
def solve(n) :
 
    # Upper limit of x & y,
    # if such x & y exists
    upper_limit = ceil(pow(n, 1.0 / 4));
 
    for x in range(upper_limit + 1) :
 
        for y in range(upper_limit + 1) :
 
            # num1 stores x^4
            num1 = x * x * x * x;
 
            # num2 stores y^4
            num2 = y * y * y * y;
 
            # If condition is satisfied
            # the print and return
            if (num1 - num2 == n) :
                print("x =", x, ", y =" , y);
                return;
 
    # If no such pair exists
    print(-1) ;
 
# Driver code
if __name__ == "__main__" :
 
    n = 15;
 
    solve(n);
     
# This code is contributed by AnkitRai01


C#
// C# implementation to find the
// values of x and y for the given
// equation with integer N
using System;
 
class GFG{
 
// Function which find required x & y
static void solve(int n)
{
     
    // Upper limit of x & y,
    // if such x & y exists
    int upper_limit = (int) (Math.Ceiling
                            (Math.Pow(n, 1.0 / 4)));
 
    for(int x = 0; x <= upper_limit; x++)
    {
       for(int y = 0; y <= upper_limit; y++)
       {
           
          // num1 stores x^4
          int num1 = x * x * x * x;
           
          // num2 stores y^4
          int num2 = y * y * y * y;
           
          // If condition is satisfied
          // the print and return
          if (num1 - num2 == n)
          {
              Console.Write("x = " + x +
                          ", y = " + y);
              return;
          }
       }
    }
     
    // If no such pair exists
    Console.Write(-1);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 15;
 
    solve(n);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
x = 2, y = 1

时间复杂度: O(sqrt(N))