Related Articles
Check if a number can be represented as product of two positive perfect cubes
• Last Updated : 12 May, 2021

Given a positive integer N, the task is to check if the given number N can be represented as the product of two positive perfect cubes or not. If it is possible, then print “Yes”. Otherwise, print “No”.

Examples:

Input: N = 216
Output: Yes
Explanation:
The given number N(= 216) can be represented as 8 * 27 =  23 * 33.
Therefore, print Yes.

Input: N = 10
Output: No

Approach: The simplest approach to solve the given problem is to store the perfect cubes of all numbers from 1 to cubic root of N in a Map and check if N can be represented as the product of two numbers present in the Map or not
Follow the steps below to solve the problem:

• Initialize an ordered map, say cubes, that stores the perfect cubes in the sorted order.
• Traverse the map and check if there exists any pair whose product is N, then print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

## 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<``int``, ``int``> 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`
Output:
`Yes`

Time Complexity: O(N1/3 * log(N))
Auxiliary Space: O(N1/3)

Efficient Approach: The above approach can also be optimized based on the observation that only perfect cubes can be represented as a product of 2 perfect cubes.

Let the two numbers be x and y such that x3 * y3= N — (1)
Equation (1) can be written as:
=> (x*y)3 = N
Taking cube root both sides,
=> x*y = (N)1/3 — (2)
For equation (2) to be true, N should be a perfect cube.

So, the problem reduces to check if N is a perfect cube or not. If found to be true, print “Yes”, else “No”.

Below is the implementation of the above approach:

## 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.`
Output:
`Yes`

Time Complexity: O(N1/3)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up