Check if a number can be represented as product of two positive perfect cubes
Last Updated :
16 Oct, 2023
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 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++
#include <bits/stdc++.h>
using namespace std;
void productOfTwoPerfectCubes( int N)
{
map< int , int > cubes;
for ( int i = 1;
i * i * i <= N; i++)
cubes[i * i * i] = i;
for ( auto itr = cubes.begin();
itr != cubes.end();
itr++) {
int firstNumber = itr->first;
if (N % itr->first == 0) {
int secondNumber = N / itr->first;
if (cubes.find(secondNumber)
!= cubes.end()) {
cout << "Yes" ;
return ;
}
}
}
cout << "No" ;
}
int main()
{
int N = 216;
productOfTwoPerfectCubes(N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static void productOfTwoPerfectCubes( int N)
{
Map<Integer, Integer> cubes = new HashMap<>();
for ( int i = 1 ; i * i * i <= N; i++)
cubes.put(i * i * i,i);
for (Map.Entry<Integer,
Integer> itr: cubes.entrySet())
{
int firstNumber = itr.getKey();
if (N % itr.getKey() == 0 )
{
int secondNumber = N / itr.getKey();
if (cubes.containsKey(secondNumber))
{
System.out.println( "Yes" );
return ;
}
}
}
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 216 ;
productOfTwoPerfectCubes(N);
}
}
|
Python3
def productOfTwoPerfectCubes(N):
cubes = {}
i = 1
while i * i * i < = N:
cubes[i * i * i] = i
i + = 1
for itr in cubes:
firstNumber = itr
if (N % itr = = 0 ):
secondNumber = N / / itr
if (secondNumber in cubes):
print ( "Yes" )
return
print ( "No" )
if __name__ = = "__main__" :
N = 216
productOfTwoPerfectCubes(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void productOfTwoPerfectCubes( int N)
{
Dictionary< int , int > cubes = new Dictionary< int , int >();
for ( int i = 1; i * i * i <= N; i++){
cubes.Add(i * i * i, i);
}
foreach (KeyValuePair< int , int > kvp in cubes)
{
int firstNumber = kvp.Key;
if (N % kvp.Key == 0) {
int secondNumber = N / kvp.Key;
if (cubes.ContainsKey(secondNumber)) {
Console.Write( "Yes" );
return ;
}
}
}
Console.Write( "No" );
}
public static void Main()
{
int N = 216;
productOfTwoPerfectCubes(N);
}
}
|
Javascript
<script>
function productOfTwoPerfectCubes(N)
{
let cubes = new Map();
for (let i = 1; i * i * i <= N; i++)
cubes.set(i * i * i, i);
for (let [key, value] of cubes.entries())
{
let firstNumber = key;
if (N % key == 0)
{
let secondNumber = N / key;
if (cubes.has(secondNumber))
{
document.write( "Yes<br>" );
return ;
}
}
}
document.write( "No<br>" );
}
let N = 216;
productOfTwoPerfectCubes(N);
</script>
|
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 is reduced 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++
#include <bits/stdc++.h>
using namespace std;
void productOfTwoPerfectCubes( int N)
{
int cube_root;
cube_root = round(cbrt(N));
if (cube_root * cube_root
* cube_root
== N) {
cout << "Yes" ;
return ;
}
else {
cout << "No" ;
return ;
}
}
int main()
{
int N = 216;
productOfTwoPerfectCubes(N);
return 0;
}
|
Java
import java.lang.*;
class GFG{
public static void productOfTwoPerfectCubes( double N)
{
double cube_root;
cube_root = Math.round(Math.cbrt(N));
if (cube_root * cube_root * cube_root == N)
{
System.out.println( "Yes" );
return ;
}
else
{
System.out.println( "No" );
return ;
}
}
public static void main(String args[])
{
double N = 216 ;
productOfTwoPerfectCubes(N);
}
}
|
Python3
def productOfTwoPerfectCubes(N):
cube_root = round ((N) * * ( 1 / 3 ))
print (cube_root)
if (cube_root * cube_root * cube_root = = N):
print ( "Yes" )
return
else :
print ( "No" )
return
if __name__ = = '__main__' :
N = 216
productOfTwoPerfectCubes(N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
public static void productOfTwoPerfectCubes( double N)
{
double cube_root;
cube_root = Math.Round(Math.Cbrt(N));
if (cube_root * cube_root * cube_root == N)
{
Console.Write( "Yes" );
return ;
}
else
{
Console.Write( "No" );
return ;
}
}
static public void Main()
{
double N = 216;
productOfTwoPerfectCubes(N);
}
}
|
Javascript
<script>
function productOfTwoPerfectCubes(N)
{
var cube_root;
cube_root = Math.round(Math.cbrt(N));
if (cube_root * cube_root * cube_root == N)
{
document.write( "Yes" );
return ;
}
else
{
document.write( "No" );
return ;
}
}
var N = 216;
productOfTwoPerfectCubes(N);
</script>
|
Time Complexity: O(N1/3)
Auxiliary Space: O(1)
Approach 3: Brute Force Approach:
The above implementation checks all possible pairs of perfect cubes (up to the cube root of N) to see if their product is equal to N. If a pair is found, then N can be expressed as the product of two perfect cubes. Otherwise, N cannot be expressed as the product of two perfect cubes. The time complexity of this implementation is O(N^(1/3)*N^(1/3)) = O(N^(2/3)), and the space complexity is O(1). This approach is not very efficient for large values of N because it performs a large number of checks, but it is simple and easy to implement.
Here is the code for above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void productOfTwoPerfectCubes( int N)
{
for ( int i=1;i<=cbrt(N);i++){
for ( int j=1;j<=cbrt(N);j++){
int val = i*i*i*j*j*j;
if (val == N){
cout<< "Yes" ;
return ;
}
}
}
cout<< "No" ;
}
int main()
{
int N = 216;
productOfTwoPerfectCubes(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void productOfTwoPerfectCubes( int N) {
for ( int i = 1 ; i <= Math.cbrt(N); i++) {
for ( int j = 1 ; j <= Math.cbrt(N); j++) {
int val = i * i * i * j * j * j;
if (val == N) {
System.out.println( "Yes" );
return ;
}
}
}
System.out.println( "No" );
}
public static void main(String[] args) {
int N = 216 ;
productOfTwoPerfectCubes(N);
}
}
|
Python3
def productOfTwoPerfectCubes(N):
for i in range ( 1 , int (N * * ( 1 / 3 )) + 1 ):
for j in range ( 1 , int (N * * ( 1 / 3 )) + 1 ):
val = i * * 3 * j * * 3
if val = = N:
print ( "Yes" )
return
print ( "No" )
N = 216
productOfTwoPerfectCubes(N)
|
C#
using System;
class Program
{
static void ProductOfTwoPerfectCubes( int N)
{
for ( int i = 1; i <= Math.Ceiling(Math.Pow(N, 1.0 / 3)); i++)
{
for ( int j = 1; j <= Math.Ceiling(Math.Pow(N, 1.0 / 3)); j++)
{
int val = i * i * i * j * j * j;
if (val == N)
{
Console.WriteLine( "Yes" );
return ;
}
}
}
Console.WriteLine( "No" );
}
static void Main()
{
int N = 216;
ProductOfTwoPerfectCubes(N);
}
}
|
Javascript
function productOfTwoPerfectCubes(N) {
for (let i = 1; i <= Math.cbrt(N); i++) {
for (let j = 1; j <= Math.cbrt(N); j++) {
let val = i * i * i * j * j * j;
if (val === N) {
console.log( "Yes" );
return ;
}
}
}
console.log( "No" );
}
let N = 216;
productOfTwoPerfectCubes(N);
|
Time Complexity: O(N1/3 * log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...