{"record":{"id":"ef283ad058b6e2c1","repo":"TheAlgorithms/Python","slug":"perfect-cube-binary-search-only-accepts-integers","errorCode":null,"errorMessage":"perfect_cube_binary_search() only accepts integers","messagePattern":"perfect_cube_binary_search\\(\\) only accepts integers","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"maths/perfect_cube.py","lineNumber":36,"sourceCode":"    Space complexity: O(1)\n\n    >>> perfect_cube_binary_search(27)\n    True\n    >>> perfect_cube_binary_search(64)\n    True\n    >>> perfect_cube_binary_search(4)\n    False\n    >>> perfect_cube_binary_search(\"a\")\n    Traceback (most recent call last):\n        ...\n    TypeError: perfect_cube_binary_search() only accepts integers\n    >>> perfect_cube_binary_search(0.1)\n    Traceback (most recent call last):\n        ...\n    TypeError: perfect_cube_binary_search() only accepts integers\n    \"\"\"\n    if not isinstance(n, int):\n        raise TypeError(\"perfect_cube_binary_search() only accepts integers\")\n    if n < 0:\n        n = -n\n    left = 0\n    right = n\n    while left <= right:\n        mid = left + (right - left) // 2\n        if mid * mid * mid == n:\n            return True\n        elif mid * mid * mid < n:\n            left = mid + 1\n        else:\n            right = mid - 1\n    return False\n\n\nif __name__ == \"__main__\":\n    import doctest\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/perfect_cube.py#L18-L54","documentation":"perfect_cube_binary_search() in maths/perfect_cube.py checks whether an integer is a perfect cube using binary search over candidate cube roots. The function explicitly rejects any input that is not an int (isinstance(n, int) is False), raising TypeError before any math is done. This is a deliberate API contract: the binary-search midpoint arithmetic (// floor division, mid*mid*mid) assumes exact integers, and floats would silently give wrong answers. Note that even float values that are mathematically integral (e.g. 27.0) are rejected.","triggerScenarios":"Calling perfect_cube_binary_search('a'), perfect_cube_binary_search(0.1), perfect_cube_binary_search(27.0), or passing any value read from input()/JSON/config that has not been converted to int. bool inputs pass (bool is a subclass of int).","commonSituations":"Feeding unparsed user input or data from json.load (which yields floats for '27.0') into the function; refactoring code that previously used a float-tolerant cube check; passing numpy scalar types (np.int64 is not a Python int for isinstance purposes on some paths).","solutions":["Convert the value to int before calling: perfect_cube_binary_search(int(n)) when you know n is integral.","If the input may be non-numeric, validate/cast at the boundary (e.g. int(input().strip()) inside try/except ValueError) rather than letting the function raise.","If float support is genuinely needed, use round/verify n == int(n) first and only then call the function."],"exampleFix":"# before\nperfect_cube_binary_search(float(value))  # TypeError\n\n# after\nperfect_cube_binary_search(int(value))","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, int) or isinstance(n, bool):\n    raise TypeError(f\"expected int, got {type(n).__name__}\")\nresult = perfect_cube_binary_search(n)","typeGuard":"def is_strict_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":null,"preventionTips":["Convert external data to int at the ingestion boundary.","Write doctest-style examples for wrong-type inputs so the contract is executable.","Remember bool passes isinstance(v, int); exclude it when it matters."],"tags":["python","type-error","input-validation","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}