1 | 'use strict'
|
2 |
|
3 | const isInteger = require('../../utils/number').isInteger
|
4 | const product = require('./product')
|
5 | function factory (type, config, load, typed) {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const combinations = typed('combinations', {
|
31 | 'number, number': function (n, k) {
|
32 | let prodrange, nMinusk
|
33 |
|
34 | if (!isInteger(n) || n < 0) {
|
35 | throw new TypeError('Positive integer value expected in function combinations')
|
36 | }
|
37 | if (!isInteger(k) || k < 0) {
|
38 | throw new TypeError('Positive integer value expected in function combinations')
|
39 | }
|
40 | if (k > n) {
|
41 | throw new TypeError('k must be less than or equal to n')
|
42 | }
|
43 |
|
44 | nMinusk = n - k
|
45 |
|
46 | if (k < nMinusk) {
|
47 | prodrange = product(nMinusk + 1, n)
|
48 | return prodrange / product(1, k)
|
49 | }
|
50 | prodrange = product(k + 1, n)
|
51 | return prodrange / product(1, nMinusk)
|
52 | },
|
53 |
|
54 | 'BigNumber, BigNumber': function (n, k) {
|
55 | let max, result, i, ii
|
56 | const one = new type.BigNumber(1)
|
57 |
|
58 | if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
|
59 | throw new TypeError('Positive integer value expected in function combinations')
|
60 | }
|
61 | if (k.gt(n)) {
|
62 | throw new TypeError('k must be less than n in function combinations')
|
63 | }
|
64 |
|
65 | max = n.minus(k)
|
66 | if (k.lt(max)) max = k
|
67 | result = one
|
68 | for (i = one, ii = n.minus(max); i.lte(ii); i = i.plus(1)) {
|
69 | result = result.times(max.plus(i)).dividedBy(i)
|
70 | }
|
71 |
|
72 | return result
|
73 | }
|
74 |
|
75 |
|
76 | })
|
77 |
|
78 | combinations.toTex = { 2: `\\binom{\${args[0]}}{\${args[1]}}` }
|
79 |
|
80 | return combinations
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function isPositiveInteger (n) {
|
89 | return n.isInteger() && n.gte(0)
|
90 | }
|
91 |
|
92 | exports.name = 'combinations'
|
93 | exports.factory = factory
|