Skip to content

fix: count divisible numbers correctly in inclusive ranges - #2034

Open
lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/inclusive-divisible-count
Open

lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/inclusive-divisible-count

Conversation

@lanqing431

@lanqing431 lanqing431 commented Nov 23, 2026 •

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

countNumbersDivisible(2, 3, 2) returns 2 although only 2 is divisible by 2. Rounding the difference between fractional endpoint quotients miscounts inclusive ranges. The early divisor-size check also excludes zero and all-negative ranges incorrectly.

Count integer multipliers between ceil(lower / |divisor|) and floor(upper / |divisor|), keeping the existing input type and range checks. Five explicit cases and an exhaustive check of 396 small ranges/divisors cover inclusive endpoints, zero, negative bounds and divisor signs.

Validation (Node.js 24.22.0): all six new regression cases fail on unchanged master, and the range test checks 396 range/divisor combinations against direct enumeration. npm test -- --maxWorkers=2 --minWorkers=1 passes all 11976 tests in 346 files. npm run check-style and git diff --check pass.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is my own contribution and does not copy another proposed fix.
  • This PR only changes the existing Maths algorithm and its tests.
  • All files remain in their existing directories.

Prepared with OpenAI Codex assistance.

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