📌  相关文章
📜  检查一个数字是否可以表示为两个正完美双二次的总和

📅  最后修改于: 2022-05-13 01:56:07.227000             🧑  作者: Mango

检查一个数字是否可以表示为两个正完美双二次的总和

给定一个正整数N ,任务是检查N是否可以写成两个完美双二次的和,即(N = X 4 + Y 4 ),其中 X 和 Y 是非负整数。如果可能,则打印Yes 。否则,打印No

例子:

朴素方法:解决给定问题的最简单方法是考虑整数XY的所有可能组合,并检查它们的双二次之和是否等于N。如果发现是真的,则打印Yes 。否则,打印No

时间复杂度: O(sqrt(N))
辅助空间: O(1)

高效方法:上述方法也可以通过使用两指针方法进行优化,其思想是在范围内找到两个数字(0, \sqrt[4]{N})          .请按照以下步骤解决此问题:

  • 初始化两个指针,将i设为0 ,将j设为\sqrt[4]{N}          .
  • 迭代一个循环,直到j小于i ,然后执行以下步骤:
    • 如果j 4的值为Ni 4N ,则打印Yes
    • 如果(i 4 + i 4 )(j 4 + j 4 )(i 4 + j 4 )的值为N ,则打印Yes
    • 如果(i 4 + j 4 )的值小于N ,则将指针i增加1 。否则,将指针j1
  • 完成上述步骤后,如果循环终止,则打印No ,因为不存在满足给定条件的此类对。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the given number
// N can be expressed as the sum of two
// biquadrates or not
string sumOfTwoBiquadrates(int N)
{
    // Base Case
    if (N == 0)
        return "Yes";
 
    // Find the range to traverse
    int j = floor(sqrt(sqrt(N)));
    int i = 1;
 
    while (i <= j) {
 
        // If x & y exists
        if (j * j * j * j == N)
            return "Yes";
        if (i * i * i * i == N)
            return "Yes";
        if (i * i * i * i
                + j * j * j * j
            == N)
            return "Yes";
 
        // If sum of powers of i and j
        // of 4 is less than N, then
        // increment the value of i
        if (i * i * i * i
                + j * j * j * j
            < N)
            i++;
 
        // Otherwise, decrement the
        // value of j
        else
            j--;
    }
 
    // If no such pairs exist
    return "No";
}
 
// Driver Code
int main()
{
    int N = 7857;
    cout << sumOfTwoBiquadrates(N);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
   
// Function to check if the given number
// N can be expressed as the sum of two
// biquadrates or not
static String sumOfTwoBiquadrates(int N)
{
   
    // Base Case
    if (N == 0)
        return "Yes";
 
    // Find the range to traverse
    int j = (int)Math.floor((double)(Math.sqrt((int)(Math.sqrt(N)))));
  
    int i = 1;
 
    while (i <= j) {
 
        // If x & y exists
        if (j * j * j * j == N)
            return "Yes";
        if (i * i * i * i == N)
            return "Yes";
        if (i * i * i * i
                + j * j * j * j
            == N)
            return "Yes";
 
        // If sum of powers of i and j
        // of 4 is less than N, then
        // increment the value of i
        if (i * i * i * i
                + j * j * j * j
            < N)
            i++;
 
        // Otherwise, decrement the
        // value of j
        else
            j--;
    }
 
    // If no such pairs exist
    return "No";
}
 
// Driver Code
public static void main(String []args)
{
    int N = 7857;
    System.out.println(sumOfTwoBiquadrates(N));
 
   
}}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
import math
 
# Function to check if the given number
# N can be expressed as the sum of two
# biquadrates or not
def sumOfTwoBiquadrates(N):
 
    # Base Case
    if (N == 0):
        return "Yes"
 
    # Find the range to traverse
    j = int(math.sqrt(math.sqrt(N)))
    i = 1
 
    while (i <= j):
 
        # If x & y exists
        if (j * j * j * j == N):
            return "Yes"
        if (i * i * i * i == N):
            return "Yes"
        if (i * i * i * i + j * j * j * j == N):
            return "Yes"
 
        # If sum of powers of i and j
        # of 4 is less than N, then
        # increment the value of i
        if (i * i * i * i + j * j * j * j < N):
            i += 1
 
        # Otherwise, decrement the
        # value of j
        else:
            j -= 1
 
    # If no such pairs exist
    return "No"
 
# Driver Code
if __name__ == "__main__":
 
    N = 7857
    print(sumOfTwoBiquadrates(N))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
   
// Function to check if the given number
// N can be expressed as the sum of two
// biquadrates or not
static string sumOfTwoBiquadrates(int N)
{
   
    // Base Case
    if (N == 0)
        return "Yes";
 
    // Find the range to traverse
    int j = (int)Math.Floor((double)(Math.Sqrt((int)(Math.Sqrt(N)))));
  
    int i = 1;
 
    while (i <= j) {
 
        // If x & y exists
        if (j * j * j * j == N)
            return "Yes";
        if (i * i * i * i == N)
            return "Yes";
        if (i * i * i * i
                + j * j * j * j
            == N)
            return "Yes";
 
        // If sum of powers of i and j
        // of 4 is less than N, then
        // increment the value of i
        if (i * i * i * i
                + j * j * j * j
            < N)
            i++;
 
        // Otherwise, decrement the
        // value of j
        else
            j--;
    }
 
    // If no such pairs exist
    return "No";
}
 
// Driver Code
public static void Main()
{
    int N = 7857;
    Console.WriteLine(sumOfTwoBiquadrates(N));
 
   
}}
 
// This code is contributed by ukasp.


Javascript



输出:
Yes

时间复杂度: O(\sqrt[4]{N})
辅助空间: O(1)