mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
module.exports = function (math) {
|
|
var util = require('../../util/index'),
|
|
|
|
BigNumber = require('bignumber.js'),
|
|
collection = require('../../type/collection'),
|
|
|
|
isNumber = util.number.isNumber,
|
|
isInteger = util.number.isInteger;
|
|
|
|
/**
|
|
* Compute the number of permutations of n items taken k at a time
|
|
*
|
|
* permutations(n)
|
|
* permutations(n, k)
|
|
*
|
|
* permutations only takes integer arguments
|
|
* the following condition must be enforced: k <= n
|
|
*
|
|
* @Param {Number} n
|
|
* @Param {Number} k
|
|
* @return {Number} permutations
|
|
*/
|
|
math.permutations = function permutations (n, k) {
|
|
var arity = arguments.length;
|
|
if (arity > 2) {
|
|
throw new math.error.ArgumentsError('permutations', arguments.length, 2);
|
|
}
|
|
|
|
if (isNumber(n)) {
|
|
if (!isInteger(n) || n < 0) {
|
|
throw new TypeError('Positive integer value enpected in function permutations');
|
|
}
|
|
|
|
// Permute n objects
|
|
if (arity == 1) {
|
|
return math.factorial(n);
|
|
}
|
|
|
|
// Permute n objects, k at a time
|
|
if (arity == 2) {
|
|
if (isNumber(k)) {
|
|
if (!isInteger(k) || k < 0) {
|
|
throw new TypeError('Positive integer value enpected in function permutations');
|
|
}
|
|
if (k > n) {
|
|
throw new TypeError('second argument k must be less than or equal to first argument n');
|
|
}
|
|
return Math.floor(math.factorial(n) / math.factorial(n-k));
|
|
}
|
|
}
|
|
}
|
|
|
|
var isPositiveInteger = function(x) {
|
|
if (!x.round().equals(x) || x.lt(0)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
if (n instanceof BigNumber) {
|
|
if (k === undefined && isPositiveInteger(n))
|
|
return math.factorial(n);
|
|
// make sure k is a BigNumber as well
|
|
k = new BigNumber(k);
|
|
if (isPositiveInteger(n) && isPositiveInteger(k)) {
|
|
return math.divide(math.factorial(n), math.factorial(math.subtract(n, k)));
|
|
}
|
|
else {
|
|
throw new TypeError('Positive integer value expected in function permutations');
|
|
}
|
|
}
|
|
|
|
throw new math.error.UnsupportedTypeError('permutations', n);
|
|
};
|
|
};
|