Skip to content

fix(maths): ensure non-composite inputs are rejected in SmithNumber - #7756

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/smith-number-composite-check
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/smith-number-composite-check

Conversation

@spidervirus

Copy link
Copy Markdown
Contributor

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.isSmithNumber did not check if the input was composite before checking the digit sum. For 0, SumOfDigits.sumOfDigits(0) and primeFactorDigitSum(0) both evaluate to 0, causing isSmithNumber(0) to erroneously return true.

  • Added a check number <= 3 to reject non-composite integers ($\le 3$, including 0, 1, and negative numbers).

  • In primeFactorDigitSum, used (long) i * i <= n to avoid integer overflow in trial division for large inputs and dynamically bound the loop to updated $n$.

  • Expanded unit tests in SmithNumberTest to 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

- 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
spidervirus marked this pull request as ready for review November 15, 2026 11:54
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.40%. Comparing base (a006205) to head (34a733b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants