Open In App

Check if number formed by joining two Numbers is Perfect Cube

Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers a and b, the task is to check whether the concatenation of a and b is a perfect cube or not.
Examples: 

Input: a = 6, b = 4 
Output: Yes 
Concatenating 6 and 4 = 64. Since 64 (= 4 x 4 x 4) is a perfect cube, the output is Yes.
Input: a = 100, b = 1 
Output: No 
Concatenating 100 and 1 = 1001. Since 1001 is not a perfect cube, the output is No. 

Approach: Since appending strings are comparatively easier than appending numbers, the simplest way to solve this problem is to convert the given integers into strings. Once the concatenated number has been obtained, it can be easily checked if it is a perfect cube or not.
Below is the implementation of the above approach.

C++




// C++ program to check if the
// concatenation of two numbers
// is a perfect cube or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number is
// a perfect Cube or not
bool isPerfectCube(int x)
{
    long double cr = round(cbrt(x));
 
    return (cr * cr * cr == x);
}
 
// Function to check if
// concatenation of two numbers
// is a perfect cube or not
void checkCube(int a, int b)
{
 
    // Convert numbers to string
    // using to_string()
    string s1 = to_string(a);
    string s2 = to_string(b);
 
    // Concatenate the numbers and
    // convert it into integer
    int c = stoi(s1 + s2);
 
    // Check if concatenated value
    // is perfect cube or not
    if (isPerfectCube(c)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int a = 6;
    int b = 4;
 
    checkCube(a, b);
 
    return 0;
}


Java




// Java program to check if the
// concatenation of two numbers
// is a perfect cube or not
import java.io.*;
public class GFG {
     
    // Function to check if a number is
    // a perfect Cube or not
    static boolean isPerfectCube(int x)
    {
        long cr = Math.round(Math.cbrt(x));
     
        return (cr * cr * cr == x);
    }
     
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
     
        // Convert numbers to string
        // using to_string()
        String s1 = Integer.toString(a);
        String s2 = Integer.toString(b);
     
        // Concatenate the numbers and
        // convert it into integer
        int c = Integer.parseInt(s1 + s2);
     
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                System.out.println("Yes");
        }
        else {
                System.out.println("No");
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int a = 6;
        int b = 4;
     
        checkCube(a, b);  
    }
}
 
// This code is contributed by Yash_R


Python 3




# Python 3 program to check if the
# concatenation of two numbers
# is a perfect cube or not
 
# Function to check if a number is
# a perfect Cube or not
def isPerfectCube(x):
    x = abs(x)
    return int(round(x ** (1. / 3))) ** 3 == x
 
# Function to check if
# concatenation of two numbers
# is a perfect cube or not
def checkCube(a, b):
 
    # Convert numbers to string
    # using to_string()
    s1 = str(a)
    s2 = str(b)
 
    # Concatenate the numbers and
    # convert it into integer
    c = int(s1 + s2)
 
    # Check if concatenated value
    # is perfect cube or not
    if (isPerfectCube(c)):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    a = 6
    b = 4
 
    checkCube(a, b)
     
# This code is contributed by Surendra_Gangwar


C#




// C# program to check if the
// concatenation of two numbers
// is a perfect cube or not
using System;
 
class GFG {
     
    // Function to check if a number is
    // a perfect Cube or not
    static bool isPerfectCube(int x)
    {
        double cr = Math.Round(Math.Cbrt(x));
     
        return (cr * cr * cr == x);
    }
     
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
     
        // Convert numbers to string
        // using to_string()
        string s1 = Convert.ToString(a);
        string s2 = Convert.ToString(b);
     
        // Concatenate the numbers and
        // convert it into integer
        int c = Convert.ToInt32(s1 + s2);
     
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                Console.WriteLine("Yes");
        }
        else {
                Console.WriteLine("No");
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        int a = 6;
        int b = 4;
     
        checkCube(a, b);
    }
}
 
// This code is contributed by AbhiThakur


Javascript




<script>
 
// JavaScript program to check if the
// concatenation of two numbers
// is a perfect cube or not
 
     
// Function to check if a number is
// a perfect Cube or not
function isPerfectCube(x)
{
    var cr = Math.round(Math.cbrt(x));
 
    return (cr * cr * cr == x);
}
 
// Function to check if
// concatenation of two numbers
// is a perfect cube or not
function checkCube(a , b)
{
 
    // Convert numbers to string
    // using to_string()
    s1 = a.toString();
    s2 = b.toString();
 
    // Concatenate the numbers and
    // convert it into integer
    var c = parseInt(s1 + s2);
 
    // Check if concatenated value
    // is perfect cube or not
    if (isPerfectCube(c)) {
            document.write("Yes");
    }
    else {
            document.write("No");
    }
}
 
// Driver Code
var a = 6;
var b = 4;
 
checkCube(a, b); 
 
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

Yes

 

Time Complexity: O(cbrt(concat(a,b)))
Auxiliary Space: O(1)



Last Updated : 20 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads