Skip to content

Fix LCM rounding - #2256

Open
qownain wants to merge 1 commit into
trekhleb:masterfrom
qownain:lcm
Open

qownain wants to merge 1 commit into
trekhleb:masterfrom
qownain:lcm

Conversation

@qownain

@qownain qownain commented Nov 20, 2026

Copy link
Copy Markdown

The current formula multiplies before dividing by the GCD. That intermediate product can lose precision even when the inputs and final result are safe integers.

For example, leastCommonMultiple(9007199254740990, 3) returns 9007199254740989. Dividing first gives the correct result, 9007199254740990.

This changes the order of operations and adds regression tests for swapped arguments and negative values. Zero handling stays the same.

Validation: ESLint passes, all 588 tests across 178 suites pass, and the LCM implementation has 100% statement, branch, function, and line coverage. The new regression test fails before the fix.

Prepared with 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