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

📅  最后修改于: 2021-04-17 14:02:42             🧑  作者: Mango

给定一个正整数N ,任务是检查N是否可以表示为两个正理想立方体之间的差。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:以解决给定问题的想法是所有数字的完美立方体从1存储到X,其中X是用于该最大整数X 3(X – 1)之间的差3至多N,在一映射并检查N是否可以表示为映射中存在的两个数字之差。
请按照以下步骤解决问题:

  • 初始化一个有序映射,例如cubes ,以按排序的顺序存储前X个自然数的完美立方体。
  • 遍历地图,并检查差值等于N的一对。如果存在任何这样的对,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the number N
// can be represented as a difference
// between two perfect cubes or not
void differenceOfTwoPerfectCubes(int N)
{
    // Stores the perfect cubes
    // of first X natural numbers
    map cubes;
 
    for (int i = 1;
         (i * i * i) - ((i - 1) * (i - 1) * (i - 1)) <= N;
         i++) {
 
        cubes[i * i * i] = 1;
    }
 
    map::iterator itr;
 
    // Traverse the map
    for (itr = cubes.begin(); itr != cubes.end(); itr++) {
 
        // Stores the first number
        int firstNumber = itr->first;
 
        // Stores the second number
        int secondNumber = N + itr->first;
 
        // Search the pair for the second
        // number to obtain difference N
        // from the Map
        if (cubes.find(secondNumber) != cubes.end()) {
            cout << "Yes";
            return;
        }
    }
 
    // If N cannot be represented
    // as difference between two
    // positive perfect cubes
    cout << "No";
}
 
// Driver Code
int main()
{
    int N = 124;
    differenceOfTwoPerfectCubes(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to check if N can be represented
  // as difference of two perfect cubes or not
  public static void differenceOfTwoPerfectCubes(int N)
  {
 
    // Stores the perfect cubes
    // of first N natural numbers
    HashMap cubes = new HashMap<>();
    for (int i = 1; (i * i * i) - ((i - 1) * (i - 1) * (i - 1)) <= N; i++)
      cubes.put((i * i * i), 1);
 
    // Traverse the map
    Iterator > itr
      = cubes.entrySet().iterator();
    while (itr.hasNext())
    {
      Map.Entry entry = itr.next();
 
      // Stores first number
      int firstNumber = entry.getKey();
 
      // Stores second number
      int secondNumber = N + entry.getKey();
 
      // Search the pair for the second
      // number to obtain differnce N from the Map
      if (cubes.containsKey(secondNumber))
      {
        System.out.println("Yes");
        return;
      }
    }
 
    // If N cannot be represented as
    // difference of two positive perfect cubes
    System.out.println("No");
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 124;
 
    // Function call to check if N
    // can be represented as
    // sum of two perfect cubes or not
    differenceOfTwoPerfectCubes(N);
  }
}
 
// This code is contributed by shailjapriya.


Python3
# Python3 program for the above approach
 
# Function to check if the number N
# can be represented as a difference
# between two perfect cubes or not
def differenceOfTwoPerfectCubes(N):
 
    # Stores the perfect cubes
    # of first X natural numbers
    cubes = {}
 
    i = 1
    while ((i * i * i) - ((i - 1) * (i - 1) * (i - 1)) <= N):
        cubes[i * i * i] = 1
        i += 1
 
    # Traverse the map
    for itr in cubes.keys():
 
        # Stores the first number
        firstNumber = itr
 
        # Stores the second number
        secondNumber = N + itr
 
        # Search the pair for the second
        # number to obtain difference N
        # from the Map
        if ((secondNumber) in cubes):
            print("Yes")
            return
 
    # If N cannot be represented
    # as difference between two
    # positive perfect cubes
    print("No")
 
# Driver Code
if __name__ == "__main__":
 
    N = 124
    differenceOfTwoPerfectCubes(N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approch
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Function to check if N can be represented
// as difference of two perfect cubes or not
public static void differenceOfTwoPerfectCubes(int N)
{
     
    // Stores the perfect cubes
    // of first N natural numbers
    Dictionary cubes = new Dictionary();
    for(int i = 1;
            (i * i * i) - ((i - 1) *
            (i - 1) * (i - 1)) <= N;
            i++)
        cubes.Add((i * i * i), 1);
 
    // Traverse the map
    foreach(KeyValuePair entry in cubes)
    {
         
        // Stores first number
        int firstNumber = entry.Key;
 
        // Stores second number
        int secondNumber = N + entry.Key;
 
        // Search the pair for the second
        // number to obtain differnce N from the Map
        if (cubes.ContainsKey(secondNumber))
        {
            Console.Write("Yes");
            return;
        }
    }
 
    // If N cannot be represented as
    // difference of two positive perfect cubes
    Console.Write("No");
}
 
// Driver code
static void Main()
{
    int N = 124;
 
    // Function call to check if N
    // can be represented as
    // sum of two perfect cubes or not
    differenceOfTwoPerfectCubes(N);
}
}
 
// This code is contributed by abhinavjain194


输出:
Yes

时间复杂度: O(∛N* log N)
辅助空间: O(∛N)