-
Notifications
You must be signed in to change notification settings - Fork 2
feat(math, perfect squares): least number pefect squares that sum to n #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughAdds a perfect-squares module (two implementations: DP and number-theory reduction), README and parameterized tests; plus a non-functional import reformat in an unrelated binary search test. Changes
Sequence Diagram(s)(omitted — changes do not introduce multi-component sequential interactions warranting a diagram) Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (3)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Nitpick comments (2)
pymath/perfect_square/README.md (1)
22-22: Fix mathematical notation formatting.The expression
4^a(8b+7)is ambiguous. It should be written as4^a * (8b+7)or4^a(8b+7)with clearer formatting to indicate multiplication between the power of 4 and the term (8b+7).🔎 Proposed fix
-three squares, and these are exactly the numbers that look like 4^a(8b+7) That means, if a number (after dividing out +three squares, and these are exactly the numbers that look like 4^a * (8b+7). That means, if a number (after dividing outpymath/perfect_square/test_perfect_squares.py (1)
6-12: Add edge case tests and expand coverage.The current test cases are valid but miss important edge cases:
- n = 0 (if supported): Should return 0
- n = 2: Requires 2 squares (1+1)
- Larger values: Consider testing with values closer to the constraint boundary (near 10³)
- Perfect squares: Additional cases like n=4, n=9, n=16 to verify they return 1
🔎 Suggested additional test cases
TEST_CASES = [ (1, 1), + (2, 2), + (4, 1), + (9, 1), (12, 3), (13, 2), (23, 4), + (100, 1), (997, 2), ]
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
algorithms/search/binary_search/divide_chocolate/test_divide_chocolate.pypymath/perfect_square/README.mdpymath/perfect_square/__init__.pypymath/perfect_square/test_perfect_squares.py
🧰 Additional context used
🧬 Code graph analysis (1)
pymath/perfect_square/test_perfect_squares.py (1)
pymath/perfect_square/__init__.py (1)
num_squares(32-49)
🪛 LanguageTool
pymath/perfect_square/README.md
[style] ~22-~22: Consider an alternative for the overused word “exactly”.
Context: ...he sum of three squares, and these are exactly the numbers that look like 4^a(8b+7) Th...
(EXACTLY_PRECISELY)
🔇 Additional comments (6)
algorithms/search/binary_search/divide_chocolate/test_divide_chocolate.py (1)
4-7: Import reformatting looks good.The multi-line parenthesized import format follows PEP 8 conventions and improves readability without affecting functionality or test behavior.
pymath/perfect_square/README.md (1)
1-51: Well-documented algorithm with clear explanation.The documentation provides excellent coverage of the problem, mathematical foundations (Four-Square and Three-Square theorems), algorithm steps, and complexity analysis. This will help maintainers understand the mathematical approach.
pymath/perfect_square/__init__.py (3)
4-15: LGTM: Type hints and docstring added.The function correctly checks for perfect squares with proper negative input handling. The type annotations and documentation improve code clarity.
40-47: Algorithm implementation is correct.The dynamic programming approach correctly implements the recurrence relation: for each number i, it tries all perfect squares j² ≤ i and takes the minimum. The time complexity O(n√n) and space complexity O(n) are appropriate for this approach.
60-77: Algorithm correctly implements mathematical theorems.The step-by-step approach based on Four-Square and Three-Square theorems is correctly implemented:
- Factor-of-4 reduction maintains the answer while simplifying computation
- The 8k+7 check correctly identifies numbers requiring 4 squares
- The perfect square and two-square checks follow the logical elimination process
Time complexity is O(√n) which is significantly better than the DP approach.
pymath/perfect_square/test_perfect_squares.py (1)
15-19: Test structure is well-organized.The use of parameterized tests makes it easy to add new test cases and keeps the test code DRY. The test method correctly validates the expected output against the actual result.
Input validation Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Describe your change:
Find least number of perfect squares that sum up to n
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
New Features
Documentation
Tests
Style
✏️ Tip: You can customize this high-level summary in your review settings.