[go: up one dir, main page]

Skip to content

Commit

Permalink
added n <= k condition to combinations function
Browse files Browse the repository at this point in the history
and also changed x to n in combinations
  • Loading branch information
daniel-levin committed Jan 6, 2014
1 parent 965f423 commit 4d72b3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/function/probability/combinations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@ module.exports = function (math) {
isInteger = util.number.isInteger;

/**
* Compute the number of combinations of x items taken k at a time
* Compute the number of combinations of n items taken k at a time
*
* combinations(x, k)
* combinations(n, k)
*
* combinations only takes integer arguments
* the following condition must be enforced: k <= x
* the following condition must be enforced: k <= n
*
* @Param {Number} x
* @Param {Number} n
* @Param {Number} k
* @return {Number} combinations
*/
math.combinations = function combinations (x, k) {
math.combinations = function combinations (n, k) {
var arity = arguments.length;
if (arity != 2) {
throw new math.error.ArgumentsError('combinations', arguments.length, 2);
}

if (isNumber(x)) {
if (!isInteger(x) || x < 0) {
throw new TypeError('Positive integer value expected in function combinations');
if (isNumber(n)) {
if (!isInteger(n) || n < 0) {
throw new TypeError('Positive integer value enpected in function combinations');
}
return parseInt(math.factorial(x) / (math.factorial(k) * math.factorial(x-k)));
if (k > n) {
throw new TypeError('k must be less than or equal to n');
}
return parseInt(math.factorial(n) / (math.factorial(k) * math.factorial(n-k)));
}
throw new math.error.UnsupportedTypeError('combinations', x);
throw new math.error.UnsupportedTypeError('combinations', n);
};
};
1 change: 1 addition & 0 deletions test/function/probability/combinations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('combinations', function() {

it('should not work with non-integer and negative input', function() {
assert.throws(function() {combinations(0.5, 3)});
assert.throws(function() {combinations(3, 5)});
});

it('should not work with the wrong number or type of arguments', function() {
Expand Down

0 comments on commit 4d72b3b

Please sign in to comment.