Repository navigation
fix(maths): ensure non-composite inputs are rejected in SmithNumber - #7756
Open
spidervirus wants to merge 1 commit into
Open
spidervirus wants to merge 1 commit into
spidervirus wants to merge 1 commit into
Conversation
- Reject numbers <= 3 as Smith numbers must be composite (greater than 3) - Fix bug where isSmithNumber(0) incorrectly returned true - Prevent integer overflow in trial division loop condition by using (long) i * i - Add comprehensive test cases covering non-composite inputs (negative numbers, 0, 1), primes, and valid Smith numbers
spidervirus
marked this pull request as ready for review
November 15, 2026 11:54
spidervirus
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 15, 2026 11:54
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7756 +/- ##
============================================
- Coverage 82.40% 82.40% -0.01%
- Complexity 8487 8489 +2
============================================
Files 846 846
Lines 26245 26244 -1
Branches 5128 5128
============================================
- Hits 21627 21626 -1
Misses 3828 3828
Partials 790 790 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
A Smith number is defined as a composite number whose sum of digits equals the sum of digits of its prime factors.
Previously,
SmithNumber.isSmithNumberdid not check if the input was composite before checking the digit sum. For0,SumOfDigits.sumOfDigits(0)andprimeFactorDigitSum(0)both evaluate to0, causingisSmithNumber(0)to erroneously returntrue.Added a check$\le 3$ , including 0, 1, and negative numbers).
number <= 3to reject non-composite integers (In$n$ .
primeFactorDigitSum, used(long) i * i <= nto avoid integer overflow in trial division for large inputs and dynamically bound the loop to updatedExpanded unit tests in
SmithNumberTestto cover non-composite numbers (negative values, 0, 1), primes (2, 3, 5, 11), non-Smith composite numbers, and valid Smith numbers.I have read CONTRIBUTING.md.
This pull request is all my own work -- I have not plagiarized it.
All filenames are in PascalCase.
All functions and variable names follow Java naming conventions.
All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
All new algorithms include a corresponding test class that validates their functionality.
All new code is formatted with
clang-format -i --style=file path/to/your/file.java