Skip to content
Related Articles
Open in App
Not now

Related Articles

Check if a number can be represented as sum of two positive perfect biquadrates

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 08 Nov, 2021
Improve Article
Save Article

Given a positive integer N, the task is to check if N can be written as the sum of two perfect biquadrates or not i.e., (N = X4 + Y4), where X and Y are non-negative integers. If it is possible, then print Yes. Otherwise, print No.

Examples:

Input: N = 97
Output: Yes
Explanation: Summation of the biquadrates of 2 and 3 is 97, i.e. 24 + 34 = 16 + 81 = 97(= N).

Input: N = 7857
Output: Yes

Naïve Approach: The simplest approach to solve the given problem is to consider all possible combinations of integers X and Y and check if the sum of their biquadrates equals N or not. If found to be true, then print Yes. Otherwise, print No.

Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by using the two-pointer approach, the idea is to find the two numbers over the range (0, \sqrt[4]{N})          . Follow the below step to solve this problem:

  • Initialize two pointers, say i as 0 and j as \sqrt[4]{N}          .
  • Iterate a loop until j becomes less than i, and perform the following steps:
    • If the value of j4 is N and i4 is N, then print Yes.
    • If the value of (i4 + i4) or (j4 + j4) or (i4 + j4) is N, then print Yes.
    • If the value of (i4 + j4) is less than N, then increment the pointer i by 1. Otherwise, decrement the pointer j by 1.
  • After completing the above steps, if the loops terminate, then print No as there exists no such pairs satisfying the given criteria.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
// Javascript program for the above approach
 
// Function to check if the given number
// N can be expressed as the sum of two
// biquadrates or not
function sumOfTwoBiquadrates(N)
{
 
  // Base Case
  if (N == 0) return "Yes";
 
  // Find the range to traverse
  let j = Math.floor(Math.sqrt(Math.sqrt(N)));
  let 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
 
let N = 7857;
document.write(sumOfTwoBiquadrates(N));
 
// This code is contributed by gfgking.
</script>


 
 

Output: 

Yes

 

 

Time Complexity: O(\sqrt[4]{N})
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!