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