Check if a number can be represented as sum of two consecutive perfect cubes

08 Apr, 2021
Given an integer N, the task is to check if this number can be represented as the sum of two consecutive perfect cubes or not.
Examples:
Input: N = 35Output: YesExplanation:Since, 35 = 23 + 33, therefore the required answer is Yes.
Input: N = 14Output: No
Naive Approach: The simplest approach to solve the problem is to iterate from 1 to cube root of N and check if the sum of perfect cubes of any two consecutive numbers is equal to N or not. If found to be true, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ Program of the// above approach #include <bits/stdc++.h>using namespace std; // Function to check if a number// can be expressed as the sum of// cubes of two consecutive numbersbool isCubeSum(int n){    for (int i = 1; i * i * i <= n; i++) {        if (i * i * i                + (i + 1) * (i + 1) * (i + 1)            == n)            return true;    }    return false;} // Driver Codeint main(){    int n = 35;     if (isCubeSum(n))        cout << "Yes";    else        cout << "No";}
// Java program of the// above approachimport java.util.*; class GFG{ // Function to check if a number// can be expressed as the sum of// cubes of two consecutive numbersstatic boolean isCubeSum(int n){    for(int i = 1; i * i * i <= n; i++)    {        if (i * i * i + (i + 1) *              (i + 1) * (i + 1) == n)            return true;    }    return false;} // Driver Codepublic static void main(String[] args){    int n = 35;     if (isCubeSum(n))        System.out.print("Yes");    else        System.out.print("No");}} // This code is contributed by Amit Katiyar
# Python3 program of the# above approach # Function to check if a number# can be expressed as the sum of# cubes of two consecutive numbersdef isCubeSum(n):         for i in range(1, int(pow(n, 1 / 3)) + 1):        if (i * i * i + (i + 1) *              (i + 1) * (i + 1) == n):            return True;     return False; # Driver Codeif __name__ == '__main__':         n = 35;     if (isCubeSum(n)):        print("Yes");    else:        print("No"); # This code is contributed by Amit Katiyar
// C# program of the// above approachusing System; class GFG{ // Function to check if a number// can be expressed as the sum of// cubes of two consecutive numbersstatic bool isCubeSum(int n){    for(int i = 1; i * i * i <= n; i++)    {        if (i * i * i + (i + 1) *              (i + 1) * (i + 1) == n)            return true;    }    return false;} // Driver Codepublic static void Main(String[] args){    int n = 35;     if (isCubeSum(n))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by Amit Katiyar
<script> // Javascript Program of the// above approach // Function to check if a number// can be expressed as the sum of// cubes of two consecutive numbersfunction isCubeSum(n){    for (var i = 1; i * i * i <= n; i++) {        if (i * i * i                + (i + 1) * (i + 1) * (i + 1)            == n)            return true;    }    return false;} // Driver Codevar n = 35;if (isCubeSum(n))    document.write("Yes");else    document.write("No");  </script>
Yes
Efficient Approach: The above approach can be optimized based on the following observations:
A number can be represented as the sum of the perfect cube of two consecutive numbers if the sum of the cube root of both consecutive numbers is equal to N.
This can be checked by the formula:
For example, if N = 35, then check of the equation below os equal to N or not:
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ Program to// implement above approach #include <bits/stdc++.h>using namespace std; // Function to check that a number// is the sum of cubes of 2// consecutive numbers or notbool isSumCube(int N){    int a = cbrt(N);    int b = a - 1;     // Condition to check if a    // number is the sum of cubes of 2    // consecutive numbers or not    return ((a * a * a + b * b * b) == N);} // Driver Codeint main(){    int i = 35;    // Function call    if (isSumCube(i)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}
// Java program to implement// above approachclass GFG{ // Function to check that a number// is the sum of cubes of 2// consecutive numbers or notstatic boolean isSumCube(int N){    int a = (int)Math.cbrt(N);    int b = a - 1;     // Condition to check if a    // number is the sum of cubes of 2    // consecutive numbers or not    return ((a * a * a + b * b * b) == N);} // Driver Codepublic static void main(String[] args){    int i = 35;         // Function call    if (isSumCube(i))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by Amit Katiyar
# Python3 program to# implement above approach # Function to check that a number# is the sum of cubes of 2# consecutive numbers or notdef isSumCube(N):     a = int(pow(N, 1 / 3))    b = a - 1     # Condition to check if a    # number is the sum of cubes of 2    # consecutive numbers or not    ans = ((a * a * a + b * b * b) == N)     return ans # Driver Codei = 35 # Function callif(isSumCube(i)):    print("Yes")else:    print("No") # This code is contributed by Shivam Singh
// C# program to implement// above approachusing System;class GFG{ // Function to check that a number// is the sum of cubes of 2// consecutive numbers or notstatic bool isSumCube(int N){  int a = (int)Math.Pow(N, (double) 1 / 3);  int b = a - 1;   // Condition to check if a  // number is the sum of cubes of 2  // consecutive numbers or not  return ((a * a * a + b * b * b) == N);} // Driver Codepublic static void Main(String[] args){  int i = 35;     // Function call  if (isSumCube(i))  {    Console.Write("Yes");  }  else  {    Console.Write("No");  }}} // This code is contributed by 29AjayKumar
<script> // Javascript program to implement// above approach    // Function to check that a number// is the sum of cubes of 2// consecutive numbers or notfunction isSumCube(N){    var a = parseInt(Math.cbrt(N));    var b = a - 1;     // Condition to check if a    // number is the sum of cubes of 2    // consecutive numbers or not    return ((a * a * a + b * b * b) == N);} // Driver Codevar i = 35; // Function callif (isSumCube(i)){    document.write("Yes");}else{    document.write("No");} // This code is contributed by todaysgaurav </script>
Yes
Time Complexity: O(1) Auxiliary Space: O(1) 
SHIVAMSINGH67
amit143katiyar
29AjayKumar
noob2000
todaysgaurav
maths-perfect-cube
maths-power
Greedy
Mathematical
Searching
Searching
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.