mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
27 lines (22 loc) • 741 B
JavaScript
import { isInteger } from '../../utils/number.js';
import { product } from '../../utils/product.js';
export function combinationsNumber(n, k) {
if (!isInteger(n) || n < 0) {
throw new TypeError('Positive integer value expected in function combinations');
}
if (!isInteger(k) || k < 0) {
throw new TypeError('Positive integer value expected in function combinations');
}
if (k > n) {
throw new TypeError('k must be less than or equal to n');
}
var nMinusk = n - k;
var prodrange;
if (k < nMinusk) {
prodrange = product(nMinusk + 1, n);
return prodrange / product(1, k);
}
prodrange = product(k + 1, n);
return prodrange / product(1, nMinusk);
}
combinationsNumber.signature = 'number, number';