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

📅  最后修改于: 2021-09-02 07:30:36             🧑  作者: Mango

给定一个正整数N ,任务是检查给定的数字N 是否可以表示为两个正完全立方体的乘积。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:解决给定问题的最简单方法是将从1N 的立方根的所有数字的完美立方体存储在 Map 中,并检查N 是否可以表示为 Map 中存在的两个数字的乘积。
请按照以下步骤解决问题:

  • 初始化一个有序映射,比如cubes ,它以排序的顺序存储完美的立方体。
  • 遍历地图并检查是否存在任何乘积为N 的对,然后打印“是” 。否则,打印“否”

下面是上述方法的实现:

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


Python3
# Python3 program for the above approach
 
# Function to check if N can
# be represented as the product
# of two perfect cubes or not
def productOfTwoPerfectCubes(N):
 
    # Stores the perfect cubes
    cubes = {}
 
    i = 1
     
    while i * i * i <= N:
        cubes[i * i * i] = i
        i += 1
 
    # Traverse the Map
    for itr in cubes:
 
        # Stores the first number
        firstNumber = itr
 
        if (N % itr == 0):
 
            # Stores the second number
            secondNumber = N // itr
 
            # Search the pair for the
            # first number to obtain
            # product N from the Map
            if (secondNumber in cubes):
                print("Yes")
                return
 
    # If N cannot be represented
    # as the product of the two
    # positive perfect cubes
    print("No")
 
# Driver Code
if __name__ == "__main__":
     
    N = 216
     
    productOfTwoPerfectCubes(N)
 
# This code is contributed by mohit ukasp


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
 // Function to check if N can
// be represented as the product
// of two perfect cubes or not
static void productOfTwoPerfectCubes(int N)
{
    // Stores the perfect cubes
    Dictionary cubes = new Dictionary();
 
    for (int i = 1; i * i * i <= N; i++){
       cubes.Add(i * i * i, i);
    }
 
    // Traverse the Map
    foreach(KeyValuePair kvp in cubes)
   {
 
        // Stores the first number
        int firstNumber = kvp.Key;
 
        if (N % kvp.Key == 0) {
 
            // Stores the second number
            int secondNumber = N / kvp.Key;
 
            // Search the pair for the
            // first number to obtain
            // product N from the Map
            if (cubes.ContainsKey(secondNumber)) {
                Console.Write("Yes");
                return;
            }
        }
    }
 
    // If N cannot be represented
    // as the product of the two
    // positive perfect cubes
    Console.Write("No");
}
 
// Driver Code
public static void Main()
{
    int N = 216;
    productOfTwoPerfectCubes(N);
 
}
}
 
// This code is contributed by ipg2016107..


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
void productOfTwoPerfectCubes(int N)
{
    int cube_root;
    cube_root = round(cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root
            * cube_root
        == N) {
 
        cout << "Yes";
        return;
    }
 
    // Otherwise, print No
    else {
        cout << "No";
        return;
    }
}
 
// Driver Code
int main()
{
    int N = 216;
    productOfTwoPerfectCubes(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.lang.*;
 
class GFG{
     
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
public static void productOfTwoPerfectCubes(double N)
{
    double cube_root;
    cube_root = Math.round(Math.cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N)
    {
        System.out.println("Yes");
        return;
    }
 
    // Otherwise, print No
    else
    {
        System.out.println("No");
        return;
    }
}
 
// Driver Code
public static void main(String args[])
{
    double N = 216;
     
    productOfTwoPerfectCubes(N);
}
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Function to check if the number N
# can be represented as the product
# of two perfect cubes or not
def productOfTwoPerfectCubes(N):
     
    cube_root = round((N) ** (1 / 3))
    print(cube_root)
 
    # If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N):
        print("Yes")
        return
     
    # Otherwise, prNo
    else:
        print("No")
        return
 
# Driver Code
if __name__ == '__main__':
     
    N = 216
     
    productOfTwoPerfectCubes(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
public static void productOfTwoPerfectCubes(double N)
{
    double cube_root;
    cube_root = Math.Round(Math.Cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N)
    {
        Console.Write("Yes");
        return;
    }
 
    // Otherwise, print No
    else
    {
        Console.Write("No");
        return;
    }
}
 
// Driver Code
static public void Main()
{
    double N = 216;
     
    productOfTwoPerfectCubes(N);
}
}
 
// This code is contributed by mohit kumar 29.


Javascript


输出:
Yes

时间复杂度: O(N 1/3 * log(N))
辅助空间: O(N 1/3 )

高效方法:上述方法也可以根据观察进行优化,即只有完美立方体可以表示为 2 个完美立方体的乘积。

因此,问题简化为检查N是否为完美立方体。如果发现是真的,打印“是” ,否则打印“否”

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
void productOfTwoPerfectCubes(int N)
{
    int cube_root;
    cube_root = round(cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root
            * cube_root
        == N) {
 
        cout << "Yes";
        return;
    }
 
    // Otherwise, print No
    else {
        cout << "No";
        return;
    }
}
 
// Driver Code
int main()
{
    int N = 216;
    productOfTwoPerfectCubes(N);
 
    return 0;
}

Java

// Java program for the above approach
import java.lang.*;
 
class GFG{
     
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
public static void productOfTwoPerfectCubes(double N)
{
    double cube_root;
    cube_root = Math.round(Math.cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N)
    {
        System.out.println("Yes");
        return;
    }
 
    // Otherwise, print No
    else
    {
        System.out.println("No");
        return;
    }
}
 
// Driver Code
public static void main(String args[])
{
    double N = 216;
     
    productOfTwoPerfectCubes(N);
}
}
 
// This code is contributed by SoumikMondal

蟒蛇3

# Python3 program for the above approach
 
# Function to check if the number N
# can be represented as the product
# of two perfect cubes or not
def productOfTwoPerfectCubes(N):
     
    cube_root = round((N) ** (1 / 3))
    print(cube_root)
 
    # If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N):
        print("Yes")
        return
     
    # Otherwise, prNo
    else:
        print("No")
        return
 
# Driver Code
if __name__ == '__main__':
     
    N = 216
     
    productOfTwoPerfectCubes(N)
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Function to check if the number N
// can be represented as the product
// of two perfect cubes or not
public static void productOfTwoPerfectCubes(double N)
{
    double cube_root;
    cube_root = Math.Round(Math.Cbrt(N));
 
    // If cube of cube_root is N
    if (cube_root * cube_root * cube_root == N)
    {
        Console.Write("Yes");
        return;
    }
 
    // Otherwise, print No
    else
    {
        Console.Write("No");
        return;
    }
}
 
// Driver Code
static public void Main()
{
    double N = 216;
     
    productOfTwoPerfectCubes(N);
}
}
 
// This code is contributed by mohit kumar 29.

Javascript


输出:
Yes

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