Skip to content

Commit 5ce584f

Browse files
committed
fix: return exact integers from binomial coefficient
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.
1 parent 5c39e87 commit 5ce584f

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎Maths/BinomialCoefficient.js‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@
1414
* @returns {number}
1515
*/
1616

17-
import { calcFactorial } from './Factorial'
18-
1917
export const findBinomialCoefficient = (n, k) => {
2018
if (typeof n !== 'number' || typeof k !== 'number') {
2119
throw Error('Type of arguments must be number.')
2220
}
2321
if (n < 0 || k < 0) {
2422
throw Error('Arguments must be greater than zero.')
2523
}
26-
let product = 1
27-
for (let i = n; i > k; i--) {
28-
product *= i
24+
if (k > n) {
25+
return 0
26+
}
27+
// Multiplicative formula in exact integer arithmetic: after step i the
28+
// running value is exactly C(n, i + 1), since C(n, i) * (n - i) is always
29+
// divisible by (i + 1). BigInt keeps every intermediate exact, unlike the
30+
// factorial formula n! / (k! * (n - k)!), whose large intermediates lose
31+
// integer precision in floating point even for small answers
32+
// (e.g. C(25, 2) evaluated to 299.99999999999994).
33+
const steps = Math.min(k, n - k)
34+
let result = 1n
35+
for (let i = 0; i < steps; i++) {
36+
result = (result * BigInt(n - i)) / BigInt(i + 1)
2937
}
30-
return product / calcFactorial(n - k)
38+
return Number(result)
3139
}

‎Maths/test/BinomialCoefficient.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ describe('Testing findBinomialCoefficient function', () => {
1111
expect(binomialCoefficient).toBe(10)
1212
})
1313

14+
it('should return exact integers for larger inputs', () => {
15+
expect(findBinomialCoefficient(25, 2)).toBe(300)
16+
expect(findBinomialCoefficient(26, 6)).toBe(230230)
17+
expect(findBinomialCoefficient(30, 13)).toBe(119759850)
18+
expect(findBinomialCoefficient(52, 5)).toBe(2598960)
19+
})
20+
21+
it('should be symmetric in k and n - k', () => {
22+
expect(findBinomialCoefficient(30, 13)).toBe(
23+
findBinomialCoefficient(30, 17)
24+
)
25+
expect(findBinomialCoefficient(40, 5)).toBe(findBinomialCoefficient(40, 35))
26+
})
27+
28+
it('should return 0 when k is greater than n', () => {
29+
expect(findBinomialCoefficient(3, 5)).toBe(0)
30+
})
31+
1432
it('should throw error when supplied arguments other than number', () => {
1533
expect(() => {
1634
findBinomialCoefficient('eight', 'three')

0 commit comments

Comments
 (0)