Repository navigation
fix: return exact integers from binomial coefficient - #2041
Open
Aditya2375 wants to merge 1 commit into
Open
Aditya2375 wants to merge 1 commit into
Aditya2375 wants to merge 1 commit into
Conversation
The factorial formula n! / (k! * (n - k)!) loses integer precision in floating point through its large intermediates, returning non-integers even for small answers, e.g. findBinomialCoefficient(25, 2) evaluated to 299.99999999999994 instead of 300. Compute the multiplicative formula in exact integer arithmetic instead: after step i the running value is exactly C(n, i + 1), and the final result is converted back to a Number. Also return 0 for k > n instead of surfacing a confusing factorial error.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2041 +/- ##
=======================================
Coverage 85.91% 85.92%
=======================================
Files 379 379
Lines 19778 19786 +8
Branches 3016 3017 +1
=======================================
+ Hits 16993 17001 +8
Misses 2785 2785 ☔ 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.
Describe your change:
findBinomialCoefficient(n, k)computesn! / (k! * (n - k)!)as one big division. The factorial intermediates overflow double-precision integer range long before the final answer does, so the function returns non-integer results for a value that must always be an integer:findBinomialCoefficient(25, 2)→299.99999999999994(should be300)findBinomialCoefficient(26, 6)→230230.00000000003(should be230230)findBinomialCoefficient(30, 13)→119759850.00000001(should be119759850)This PR computes the multiplicative formula in exact integer arithmetic instead: after step
ithe running value is exactlyC(n, i + 1)(sinceC(n, i) * (n - i)is always divisible byi + 1), and only the final result is converted back to aNumber. Verified against an exact arbitrary-precision oracle for every0 <= k <= n <= 70with zero mismatches.Also returns
0fork > ninstead of surfacing a confusing "factorial does not exist" error from the helper.Added regression tests for the exact-integer cases above, symmetry (
C(n, k) === C(n, n - k)), andk > n.Checklist:
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.