📌  相关文章
📜  检查一个数字是否可以表示为两个连续的完美立方体的总和

📅  最后修改于: 2021-04-22 08:47:40             🧑  作者: Mango

给定一个整数N ,任务是检查此数字是否可以表示为两个连续的完美立方体的总和。

例子:

天真的方法:解决问题的最简单方法是从1迭代到N的立方根,并检查任意两个连续数字的理想立方的总和是否等于N。如果发现是真的,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ Program of the
// above approach
 
#include 
using namespace std;
 
// Function to check if a number
// can be expressed as the sum of
// cubes of two consecutive numbers
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 Code
int main()
{
    int n = 35;
 
    if (isCubeSum(n))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program of the
// above approach
import java.util.*;
 
class GFG{
 
// Function to check if a number
// can be expressed as the sum of
// cubes of two consecutive numbers
static 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 Code
public 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
# Python3 program of the
# above approach
 
# Function to check if a number
# can be expressed as the sum of
# cubes of two consecutive numbers
def 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 Code
if __name__ == '__main__':
     
    n = 35;
 
    if (isCubeSum(n)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Amit Katiyar


C#
// C# program of the
// above approach
using System;
 
class GFG{
 
// Function to check if a number
// can be expressed as the sum of
// cubes of two consecutive numbers
static 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 Code
public 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


Javascript


C++
// C++ Program to
// implement above approach
 
#include 
using namespace std;
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
bool 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 Code
int main()
{
    int i = 35;
    // Function call
    if (isSumCube(i)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program to implement
// above approach
class GFG{
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
static 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 Code
public 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
# Python3 program to
# implement above approach
 
# Function to check that a number
# is the sum of cubes of 2
# consecutive numbers or not
def 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 Code
i = 35
 
# Function call
if(isSumCube(i)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// above approach
using System;
class GFG{
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
static 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 Code
public 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


Javascript


输出:
Yes

高效的方法:可以基于以下观察来优化上述方法:

  • 如果两个连续数的立方根的总和等于N,则数字可以表示为两个连续数的理想立方的和。
  • 可以通过以下公式检查:
  • 例如,如果N = 35 ,则检查os以下等于N的方程是否:

下面是上述方法的实现:

C++

// C++ Program to
// implement above approach
 
#include 
using namespace std;
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
bool 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 Code
int main()
{
    int i = 35;
    // Function call
    if (isSumCube(i)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java

// Java program to implement
// above approach
class GFG{
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
static 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 Code
public 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

# Python3 program to
# implement above approach
 
# Function to check that a number
# is the sum of cubes of 2
# consecutive numbers or not
def 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 Code
i = 35
 
# Function call
if(isSumCube(i)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shivam Singh

C#

// C# program to implement
// above approach
using System;
class GFG{
 
// Function to check that a number
// is the sum of cubes of 2
// consecutive numbers or not
static 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 Code
public 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

Java脚本


输出:
Yes

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