Check if a number can be represented as sum of two positive perfect biquadrates
Last Updated :
17 Apr, 2023
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
Approach 1: Naïve
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)
Approach 2: Binary Search
we can use binary search to optimize the search for pairs of biquadrates that sum up to the given number. In this program, we first create a vector of all possible biquadrates up to the square root of the square root of N. Then, we iterate through this vector and for each biquadrate, we check if its complement (i.e., N minus the biquadrate) exists in the vector using binary search. If we find such a complement, we return “Yes”. If we don’t find any such pair, we return “No”.
C++
#include <bits/stdc++.h>
using namespace std;
string sumOfTwoBiquadrates( int N)
{
if (N == 0)
return "Yes" ;
int j = floor ( sqrt ( sqrt (N)));
int i = 1;
vector< int > biquadrates;
for ( int x = 1; x <= j; x++) {
int biquadrate = x * x * x * x;
biquadrates.push_back(biquadrate);
}
for ( int i = 0; i < biquadrates.size(); i++) {
int complement = N - biquadrates[i];
if (binary_search(biquadrates.begin(),
biquadrates.end(), complement)) {
return "Yes" ;
}
}
return "No" ;
}
int main()
{
int N = 7857;
cout << sumOfTwoBiquadrates(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static String sumOfTwoBiquadrates( int N)
{
if (N == 0 )
return "Yes" ;
int j = ( int )Math.floor(Math.sqrt(Math.sqrt(N)));
int i = 1 ;
List<Integer> biquadrates
= new ArrayList<Integer>();
for ( int x = 1 ; x <= j; x++) {
int biquadrate = x * x * x * x;
biquadrates.add(biquadrate);
}
for (i = 0 ; i < biquadrates.size(); i++) {
int complement = N - biquadrates.get(i);
if (Collections.binarySearch(biquadrates,
complement)
>= 0 ) {
return "Yes" ;
}
}
return "No" ;
}
public static void main(String[] args)
{
int N = 7857 ;
System.out.println(sumOfTwoBiquadrates(N));
}
}
|
Python3
import math
def sumOfTwoBiquadrates(N):
if N = = 0 :
return "Yes"
j = math.floor(math.sqrt(math.sqrt(N)))
i = 1
biquadrates = []
for x in range ( 1 , j + 1 ):
biquadrate = x * x * x * x
biquadrates.append(biquadrate)
for i in range ( len (biquadrates)):
complement = N - biquadrates[i]
if complement in biquadrates:
return "Yes"
return "No"
N = 7857
print (sumOfTwoBiquadrates(N))
|
Javascript
function sumOfTwoBiquadrates(N) {
if (N === 0) {
return "Yes" ;
}
const j = Math.floor(Math.sqrt(Math.sqrt(N)));
let i = 1;
const biquadrates = [];
for (let x = 1; x <= j; x++) {
const biquadrate = x * x * x * x;
biquadrates.push(biquadrate);
}
for (let i = 0; i < biquadrates.length; i++) {
const complement = N - biquadrates[i];
if (biquadrates.includes(complement)) {
return "Yes" ;
}
}
return "No" ;
}
const N = 7857;
console.log(sumOfTwoBiquadrates(N));
|
C#
using System;
using System.Collections.Generic;
namespace ConsoleApp {
class Program {
static string SumOfTwoBiquadrates( int N)
{
if (N == 0)
return "Yes" ;
int j = ( int )Math.Floor(Math.Sqrt(Math.Sqrt(N)));
int i = 1;
List< int > biquadrates = new List< int >();
for ( int x = 1; x <= j; x++) {
int biquadrate = x * x * x * x;
biquadrates.Add(biquadrate);
}
foreach ( int biquadrate in biquadrates)
{
int complement = N - biquadrate;
if (biquadrates.BinarySearch(complement) >= 0) {
return "Yes" ;
}
}
return "No" ;
}
static void Main( string [] args)
{
int N = 7857;
Console.WriteLine(SumOfTwoBiquadrates(N));
}
}
}
|
Output:
Yes
Time Complexity: O(sqrt(N) * logN), where N is the given number.
Auxiliary Space: O(1)
Approach 3: Two Pointer (Efficient)
The above approach can also be optimized by using the two-pointer approach, the idea is to find the two numbers over the range . Follow the below step to solve this problem:
- Initialize two pointers, say i as 0 and j as .
- 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++
#include <bits/stdc++.h>
using namespace std;
string sumOfTwoBiquadrates( int N)
{
if (N == 0)
return "Yes" ;
int j = floor ( sqrt ( sqrt (N)));
int i = 1;
while (i <= j) {
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 (i * i * i * i + j * j * j * j < N)
i++;
else
j--;
}
return "No" ;
}
int main()
{
int N = 7857;
cout << sumOfTwoBiquadrates(N);
return 0;
}
|
Java
public class GFG
{
static String sumOfTwoBiquadrates( int N)
{
if (N == 0 )
return "Yes" ;
int j = ( int )Math.floor(( double )(Math.sqrt(( int )(Math.sqrt(N)))));
int i = 1 ;
while (i <= j) {
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 (i * i * i * i
+ j * j * j * j
< N)
i++;
else
j--;
}
return "No" ;
}
public static void main(String []args)
{
int N = 7857 ;
System.out.println(sumOfTwoBiquadrates(N));
}}
|
Python3
import math
def sumOfTwoBiquadrates(N):
if (N = = 0 ):
return "Yes"
j = int (math.sqrt(math.sqrt(N)))
i = 1
while (i < = j):
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 (i * i * i * i + j * j * j * j < N):
i + = 1
else :
j - = 1
return "No"
if __name__ = = "__main__" :
N = 7857
print (sumOfTwoBiquadrates(N))
|
Javascript
<script>
function sumOfTwoBiquadrates(N)
{
if (N == 0) return "Yes" ;
let j = Math.floor(Math.sqrt(Math.sqrt(N)));
let i = 1;
while (i <= j)
{
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 (i * i * i * i + j * j * j * j < N) i++;
else j--;
}
return "No" ;
}
let N = 7857;
document.write(sumOfTwoBiquadrates(N));
</script>
|
C#
using System;
class GFG {
static string sumOfTwoBiquadrates( int N)
{
if (N == 0)
return "Yes" ;
int j = ( int )Math.Floor(
( double )(Math.Sqrt(( int )(Math.Sqrt(N)))));
int i = 1;
while (i <= j) {
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 (i * i * i * i + j * j * j * j < N)
i++;
else
j--;
}
return "No" ;
}
public static void Main()
{
int N = 7857;
Console.WriteLine(sumOfTwoBiquadrates(N));
}
}
|
Time Complexity:
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...