Skip to content

fix(math): preserve safe integer arithmetic and bound GCD work - #2243

Open
Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/safe-integer-math
Open

Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/safe-integer-math

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

Fixes #2238.

Safe integer inputs can currently produce fractional LCM results and incorrect Pascal coefficients because intermediate products exceed JavaScript's exact integer range. The iterative GCD also performs an impractical number of subtractions for widely separated inputs.

Divide by the GCD before multiplying for LCM, retain exact BigInt intermediate coefficients for Pascal rows, and use remainder steps in the iterative Euclidean algorithm. Pascal rows still return numbers; results outside the safe integer range retain Number's precision limitations.

Validation: all 178 test suites / 593 tests pass with coverage, and ESLint passes. New precision regressions fail before the changes. An independent BigInt oracle also matched 40,402 GCD cases; the original implementation times out on the large unbalanced regression.

AI assistance was used to investigate, implement, and validate this change.

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.

math: uniquePaths and leastCommonMultiple return non-integers below 2^53; iterative Euclid is subtraction-based and can hang

1 participant