1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createCombinations = void 0;
|
7 |
|
8 | var _factory = require("../../utils/factory");
|
9 |
|
10 | var _combinations = require("../../plain/number/combinations");
|
11 |
|
12 | var name = 'combinations';
|
13 | var dependencies = ['typed'];
|
14 | var createCombinations = (0, _factory.factory)(name, dependencies, function (_ref) {
|
15 | var typed = _ref.typed;
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | return typed(name, {
|
41 | 'number, number': _combinations.combinationsNumber,
|
42 | 'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
|
43 | var BigNumber = n.constructor;
|
44 | var result, i;
|
45 | var nMinusk = n.minus(k);
|
46 | var one = new BigNumber(1);
|
47 |
|
48 | if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
|
49 | throw new TypeError('Positive integer value expected in function combinations');
|
50 | }
|
51 |
|
52 | if (k.gt(n)) {
|
53 | throw new TypeError('k must be less than n in function combinations');
|
54 | }
|
55 |
|
56 | result = one;
|
57 |
|
58 | if (k.lt(nMinusk)) {
|
59 | for (i = one; i.lte(nMinusk); i = i.plus(one)) {
|
60 | result = result.times(k.plus(i)).dividedBy(i);
|
61 | }
|
62 | } else {
|
63 | for (i = one; i.lte(k); i = i.plus(one)) {
|
64 | result = result.times(nMinusk.plus(i)).dividedBy(i);
|
65 | }
|
66 | }
|
67 |
|
68 | return result;
|
69 | }
|
70 |
|
71 | });
|
72 | });
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | exports.createCombinations = createCombinations;
|
80 |
|
81 | function isPositiveInteger(n) {
|
82 | return n.isInteger() && n.gte(0);
|
83 | } |
\ | No newline at end of file |