Repository navigation
Conversation
math.sqrt(num) * math.sqrt(num) == num reports many non-squares as squares (11, 14, 17, ...), misses squares above 2**53 and raises ValueError for negative input. Compare the exact integer square root instead.
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
Problem
maths/perfect_square.pyperfect_square()decides squareness with floats:The product of two rounded square roots often rounds back to exactly
num, so many non-squares are reported as squares. Above 2**53 the float root loses precision and real squares are missed, and any negative input raisesValueErrorfrommath.sqrtinstead of returningFalse.Reproduced on free-threaded CPython 3.15.2 (the version in
.python-version) at30507bb:Over
0..999, 513 non-squares come backTrue(11, 14, 17, 21, 22, 27, 30, ...). Over0..20000, 10,369 do.Fix
Use the exact integer square root, which needs no float rounding:
This is a code-only change. The existing doctests (9, 16, 1, 0 →
True; 10 →False) are untouched and still pass.perfect_square_binary_search()in the same file is not changed.Behaviour change, checked against master
11,14,17TrueFalse(2**30 + 1) ** 2FalseTrue-4ValueErrorFalse0,1,9,16,10Swept
-50..20000, 300 squares near 2**52, plus(2**53 + 1) ** 2,10**16 + 1and(10**20 + 7) ** 2: every changed output was a wrong answer on master, and the patched function agrees withmath.isqrt(n) ** 2 == non all of them.One more change: a float argument (the signature is
num: int) now raisesTypeErrorfrommath.isqrtinstead of being compared as a float, soperfect_square(2.25)no longer returnsTrue.Validation (free-threaded CPython 3.15.2, macOS arm64)
False, all of0..9999againstmath.isqrt,(2**30 + 1) ** 2→True,-4→False) fails on master and passes with this change. I kept it out of this PR because AGENTS.md asks not to change code and its doctests in the same PR. I can open the doctests as a follow-up PR if that helps.uv run --with=pytest-run-parallel pytest --iterations=8 --parallel-threads=auto --ignore-gil-enabled maths/perfect_square.py: all pass.pytestcommand frombuild.ymlgives the same result as on master (4139 passed, plus the same 2 environment-dependent failures as master on macOS: thegaussian_distributionRNG doctest and the network-dependentinstagram_crawlerdoctest).uvx pre-commit run --files maths/perfect_square.py: all hooks pass.uvx ruff check .: all checks passed.Checklist
🤖 Generated with Claude Code