Repository navigation
Conversation
perfect_cube cubed the float cube root, which is not exact: 64 ** (1 / 3) is 3.9999999999999996, so almost every cube (4**3, 5**3, ...) was reported as not a cube, and a negative number went through a complex root, so -64 was False while -24 was True. Round the cube root of abs(n) to an integer and compare its cube with abs(n).
mindaugl
approved these changes
Nov 21, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change
maths/perfect_cube.py::perfect_cubecubed the float cube root and compared it withn. That is not exact:64 ** (1 / 3)is3.9999999999999996, so almost every cube was reported as not a cube. Ofi**3foriin 0..199, 196 returnedFalse(only 0, 1, 2 and 3 passed); the only doctest was27. A negativenwent through a complex root:perfect_cube(-64)wasFalseandperfect_cube(-24)wasTrue.The cube root of
abs(n)is now rounded to an integer and its cube is compared withabs(n), so negative cubes count as cubes, like inperfect_cube_binary_searchin the same file. I added doctests for the cases above (they fail onmaster); the added doctests are the regression evidence for this one function, so I kept them in the same PR.I checked
-3000..3000against an exact integer search andk**3/k**3 + 1forkup to 10^5: no mismatches.ruff check,ruff format --checkandpython -m doctestpass.Written with the help of Claude Code (Claude Sonnet 5.5).
Checklist