Related Articles

Related Articles

Check if number formed by joining two Numbers is Perfect Cube
  • Difficulty Level : Easy
  • Last Updated : 16 Apr, 2020

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if the
// concatenation of two numbers
// is a perfect cube or not
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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :