UNPKG

1.33 kBJavaScriptView Raw
1'use strict';
2
3function factory(type, config, load, typed) {
4 var add = load(require('../arithmetic/add'));
5 var divide = load(require('../arithmetic/divide'));
6 var multiply = load(require('../arithmetic/multiply'));
7 var combinations = load(require('../probability/combinations'));
8 var isNegative = load(require('../utils/isNegative'));
9 var isInteger = load(require('../utils/isInteger'));
10
11 /**
12 * The Catalan Numbers enumerate combinatorial structures of many different types.
13 * catalan only takes integer arguments.
14 * The following condition must be enforced: n >= 0
15 *
16 * Syntax:
17 *
18 * math.catalan(n)
19 *
20 * Examples:
21 *
22 * math.catalan(3) // returns 5
23 * math.catalan(8) // returns 1430
24 *
25 * See also:
26 *
27 * bellNumbers
28 *
29 * @param {Number | BigNumber} n nth Catalan number
30 * @return {Number | BigNumber} Cn(n)
31 */
32 var catalan = typed('catalan', {
33 'number | BigNumber': function numberBigNumber(n) {
34 if (!isInteger(n) || isNegative(n)) {
35 throw new TypeError('Non-negative integer value expected in function catalan');
36 }
37
38 return divide(combinations(multiply(n, 2), n), add(n, 1));
39 }
40 });
41
42 catalan.toTex = { 1: '\\mathrm{C}_{${args[0]}}' };
43
44 return catalan;
45}
46
47exports.name = 'catalan';
48exports.factory = factory;
\No newline at end of file