check if number is perfect cube python code example

Example 1: check if a number is perfect cube in python

x = int(input())
print(int(round(x ** (1. / 3))) ** 3 == x)

Example 2: check if number is perfect cube python

def is_cube(n):
    guess = n**(1.0/3.0)
    iguess = int(guess)
    if iguess * iguess * iguess == n:
        print(True, "it's cubed root is", iguess)
        return
    iguess = iguess + 1
    if iguess * iguess * iguess == n:
        print(True, "it's cubed root is", iguess)
        return
    print(False, "it's cubed root is", guess)
    
is_cube(9) # No
is_cube(27) # Yes

Tags:

Misc Example