1 | import { factory } from '../../utils/factory.js';
|
2 | import { isInteger } from '../../utils/number.js';
|
3 | import { product } from '../../utils/product.js';
|
4 | var name = 'combinationsWithRep';
|
5 | var dependencies = ['typed'];
|
6 | export var createCombinationsWithRep = factory(name, dependencies, (_ref) => {
|
7 | var {
|
8 | typed
|
9 | } = _ref;
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | return typed(name, {
|
35 | 'number, number': function numberNumber(n, k) {
|
36 | if (!isInteger(n) || n < 0) {
|
37 | throw new TypeError('Positive integer value expected in function combinationsWithRep');
|
38 | }
|
39 |
|
40 | if (!isInteger(k) || k < 0) {
|
41 | throw new TypeError('Positive integer value expected in function combinationsWithRep');
|
42 | }
|
43 |
|
44 | if (n < 1) {
|
45 | throw new TypeError('k must be less than or equal to n + k - 1');
|
46 | }
|
47 |
|
48 | if (k < n - 1) {
|
49 | var _prodrange = product(n, n + k - 1);
|
50 |
|
51 | return _prodrange / product(1, k);
|
52 | }
|
53 |
|
54 | var prodrange = product(k + 1, n + k - 1);
|
55 | return prodrange / product(1, n - 1);
|
56 | },
|
57 | 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
|
58 | var BigNumber = n.constructor;
|
59 | var result, i;
|
60 | var one = new BigNumber(1);
|
61 | var nMinusOne = n.minus(one);
|
62 |
|
63 | if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
|
64 | throw new TypeError('Positive integer value expected in function combinationsWithRep');
|
65 | }
|
66 |
|
67 | if (n.lt(one)) {
|
68 | throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep');
|
69 | }
|
70 |
|
71 | result = one;
|
72 |
|
73 | if (k.lt(nMinusOne)) {
|
74 | for (i = one; i.lte(nMinusOne); i = i.plus(one)) {
|
75 | result = result.times(k.plus(i)).dividedBy(i);
|
76 | }
|
77 | } else {
|
78 | for (i = one; i.lte(k); i = i.plus(one)) {
|
79 | result = result.times(nMinusOne.plus(i)).dividedBy(i);
|
80 | }
|
81 | }
|
82 |
|
83 | return result;
|
84 | }
|
85 | });
|
86 | });
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function isPositiveInteger(n) {
|
94 | return n.isInteger() && n.gte(0);
|
95 | } |
\ | No newline at end of file |