Skip to content

Use exact roots to recognize and advance large perfect squares - #2782

Open
pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_exact_next_square
Open

pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_exact_next_square

Conversation

@pralav-25

@pralav-25 pralav-25 commented Sep 27, 2026 •

Copy link
Copy Markdown

Both next-square functions can classify 10**32 + 1 as a square, round the successor incorrectly, or overflow for large integers. Use math.isqrt and exact square verification before returning the integer successor. Non-integral floats remain nonsquares.

Adds constructed large squares and their adjacent integers, ordinary integer/float inputs, a rounded-root float regression, and exhaustive comparison against constructed squares over 0–10,000. Updates the input annotations and complexity description.

Validation on Python 3.12.14:

  • The new test file reproduced 10 failing cases on unmodified main.
  • python -m pytest -q: 626 passed.
  • python -m pytest --doctest-modules algorithms/ -q: 480 passed, 1 skipped.
  • ruff check algorithms/ tests/ and git diff --check: passed.

Prepared with AI assistance; the failures and fixes above were reproduced locally.

Summary by CodeRabbit

  • Bug Fixes
    • Improved perfect-square detection for large values, adjacent integers, and both integer and floating-point inputs. Non-square values continue to return -1.
  • Tests
    • Added coverage for common inputs, large values, and a range of integer results.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 4bfcdf11-da3b-4c47-bee2-132b0724f6d9

📥 Commits

Reviewing files that changed from the base of the PR and between bd1d152 and 672c84e.

📒 Files selected for processing (2)
  • algorithms/math/next_perfect_square.py
  • tests/test_next_square_precision.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.


📝 Walkthrough

Walkthrough

Both find_next_square functions now accept integer and float inputs and use integer square roots with exact square checks. The module describes arithmetic cost in terms of input bit length. New tests cover large values, adjacent values, selected inputs, and integers from 0 through 10,000.

Changes

Next perfect square

Layer / File(s) Summary
Function contracts, exact checks, and tests
algorithms/math/next_perfect_square.py, tests/test_next_square_precision.py
Both functions accept `int

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: keon

Merge Risk: ⚪ Minimal · up to 672c8

The exact-square changes and their tests show no identified issue that needs resolution before merge.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 672c8

The exported functions now recognize large perfect squares exactly. No new endpoint, privileged operation, or security finding is established, but their use in deployed applications is not known.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Any input-dependent computation cost is borne by a caller of the exported functions; the available dependency evidence does not identify an attacker-reachable service caller.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: using exact integer roots to identify and advance large perfect squares.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant