check if a number is a perfect cube or not perfectcube27 true perfectcube4 false check if a number is a perfect cube or not using binary search time complexity ologn space complexity o1 perfectcubebinarysearch27 true perfectcubebinarysearch64 true perfectcubebinarysearch4 false perfectcubebinarysearcha traceback most recent call last typeerror perfectcubebinarysearch only accepts integers perfectcubebinarysearch0 1 traceback most recent call last typeerror perfectcubebinarysearch only accepts integers check if a number is a perfect cube or not perfect_cube 27 true perfect_cube 4 false check if a number is a perfect cube or not using binary search time complexity o log n space complexity o 1 perfect_cube_binary_search 27 true perfect_cube_binary_search 64 true perfect_cube_binary_search 4 false perfect_cube_binary_search a traceback most recent call last typeerror perfect_cube_binary_search only accepts integers perfect_cube_binary_search 0 1 traceback most recent call last typeerror perfect_cube_binary_search only accepts integers
def perfect_cube(n: int) -> bool:
    
    val = n ** (1 / 3)
    return (val * val * val) == n


def perfect_cube_binary_search(n: int) -> bool:
    
    if not isinstance(n, int):
        raise TypeError("perfect_cube_binary_search() only accepts integers")
    if n < 0:
        n = -n
    left = 0
    right = n
    while left <= right:
        mid = left + (right - left) // 2
        if mid * mid * mid == n:
            return True
        elif mid * mid * mid < n:
            left = mid + 1
        else:
            right = mid - 1
    return False


if __name__ == "__main__":
    import doctest

    doctest.testmod()