Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0e87c4

Browse files
waseemyusufjosdejong
authored andcommitted
Make combinationsWithRep.js more efficient (josdejong#1602)
* Implemented the `multicombinations` function * Write unit tests for `multicombinations` function * Integrate the `multicombinations` function throughout codebase 1. Include multicombinations factory function in factoriesAny.js and factoriesNumber.js 2. Write embedded docs for multicombinations 3. Added latex support 4. Refer to multicombinations in the "see also"-section of related functions * Change name from `multichoose` to `combinationsWithRep` * Update combinationsWithRep.js Instead of always cancelling `n-1 factorial` from the denominator and ignoring `k factorial`, added a conditional to cancel the the larger of the two, therefore further reducing redundant calculations. * Revert "Update combinationsWithRep.js" This reverts commit efef6d3. * Update combinationsWithRep.js Instead of always cancelling `n-1 factorial` from the denominator and ignoring `k factorial`, added a conditional to cancel the the larger of the two, therefore further reducing redundant calculations. * Add one more test for the case: k > n-1 * Refactor single for-loop into two separate ones
1 parent 84dc8a3 commit c0e87c4

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/function/probability/combinationsWithRep.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@ export const createCombinationsWithRep = /* #__PURE__ */ factory(name, dependenc
4141
throw new TypeError('k must be less than or equal to n + k - 1')
4242
}
4343

44-
const prodrange = product(n, n + k - 1)
45-
return prodrange / product(1, k)
44+
if (k < n - 1) {
45+
const prodrange = product(n, n + k - 1)
46+
return prodrange / product(1, k)
47+
}
48+
const prodrange = product(k + 1, n + k - 1)
49+
return prodrange / product(1, n - 1)
4650
},
4751

4852
'BigNumber, BigNumber': function (n, k) {
4953
const BigNumber = n.constructor
5054
let result, i
5155
const one = new BigNumber(1)
56+
const nMinusOne = n.minus(one)
5257

5358
if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
5459
throw new TypeError('Positive integer value expected in function combinationsWithRep')
@@ -57,10 +62,15 @@ export const createCombinationsWithRep = /* #__PURE__ */ factory(name, dependenc
5762
throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep')
5863
}
5964

60-
const max = n.minus(one)
6165
result = one
62-
for (i = one; i.lte(k); i = i.plus(1)) {
63-
result = result.times(max.plus(i)).dividedBy(i)
66+
if (k.lt(nMinusOne)) {
67+
for (i = one; i.lte(nMinusOne); i = i.plus(one)) {
68+
result = result.times(k.plus(i)).dividedBy(i)
69+
}
70+
} else {
71+
for (i = one; i.lte(k); i = i.plus(one)) {
72+
result = result.times(nMinusOne.plus(i)).dividedBy(i)
73+
}
6474
}
6575

6676
return result

test/unit-tests/function/probability/combinationsWithRep.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ const combinationsWithRep = math.combinationsWithRep
55
describe('combinations', function () {
66
it('should calculate the combinations of a number taking k at a time', function () {
77
assert.strictEqual(combinationsWithRep(7, 5), 462)
8+
assert.strictEqual(combinationsWithRep(3, 10), 66)
89
assert.strictEqual(combinationsWithRep(8, 33), 18643560)
910
assert.strictEqual(combinationsWithRep(63, 7), 1078897248)
1011
assert.strictEqual(combinationsWithRep(25, 6), 593775)
1112
})
1213

1314
it('should calculate the combinations of n items taken k at a time with BigNumbers', function () {
1415
assert.deepStrictEqual(combinationsWithRep(math.bignumber(7), math.bignumber(5)), math.bignumber(462))
16+
assert.deepStrictEqual(combinationsWithRep(math.bignumber(3), math.bignumber(10)), math.bignumber(66))
1517
assert.deepStrictEqual(combinationsWithRep(math.bignumber(8), math.bignumber(33)), math.bignumber(18643560))
1618
assert.deepStrictEqual(combinationsWithRep(math.bignumber(63), math.bignumber(7)), math.bignumber(1078897248))
1719
assert.deepStrictEqual(combinationsWithRep(math.bignumber(25), math.bignumber(6)), math.bignumber(593775))

0 commit comments

Comments
 (0)