Comprueba si el número formado al unir dos Números es Perfect Cube

Dados dos números a y b , la tarea es verificar si la concatenación de a y b es un cubo perfecto o no.
Ejemplos: 

Entrada: a = 6, b = 4 
Salida: Sí 
Concatenando 6 y 4 = 64. Dado que 64 (= 4 x 4 x 4) es un cubo perfecto, la salida es Sí.
Entrada: a = 100, b = 1 
Salida: No 
Concatenar 100 y 1 = 1001. Dado que 1001 no es un cubo perfecto, la salida es No. 

Enfoque: dado que agregar strings es comparativamente más fácil que agregar números, la forma más sencilla de resolver este problema es convertir los números enteros dados en strings . Una vez obtenido el número concatenado, se puede comprobar fácilmente si es un cubo perfecto o no .
A continuación se muestra la implementación del enfoque anterior.

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
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>
Producción: 

Yes

 

Complejidad de tiempo: O(cbrt(concat(a,b)))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *