Add binomialCoefficient

This commit is contained in:
Angelos Chalaris
2018-02-14 12:34:02 +02:00
parent 4a7534f44c
commit f1086e8a45
5 changed files with 68 additions and 3 deletions

View File

@ -0,0 +1,26 @@
### binomialCoefficient
Evaluates the binomial coefficient of two integers `n` and `k`.
Use `Number.isNaN()` to check if any of the two values is `NaN`.
Check if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result.
Check if `n - k` is less than `k` and switch their values accordingly.
Loop from `2` through `k` and calculate the binomial coefficient.
Use `Math.round()` to account for rounding errors in the calculation.
```js
const binomialCoefficient = (n, k) => {
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
if (k === 1 || k === n - 1) return n;
if (n - k < k) k = n - k;
let res = n;
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
return Math.round(res);
};
```
```js
binomialCoefficient(8, 2); // 28
```

View File

@ -10,6 +10,7 @@ bifurcateBy:array,function
bind:function,object
bindAll:object,function
bindKey:function,object
binomialCoefficient:math
bottomVisible:browser
btoa:node,string,utility
byteSize:string

View File

@ -0,0 +1,11 @@
const binomialCoefficient = (n, k) => {
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
if (k === 1 || k === n - 1) return n;
if (n - k < k) k = n - k;
let res = n;
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
return Math.round(res);
};
module.exports = binomialCoefficient;

View File

@ -0,0 +1,18 @@
const test = require('tape');
const binomialCoefficient = require('./binomialCoefficient.js');
test('Testing binomialCoefficient', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof binomialCoefficient === 'function', 'binomialCoefficient is a Function');
t.equal(binomialCoefficient(8, 2), 28, 'Returns the appropriate value');
t.equal(binomialCoefficient(0, 0), 1, 'Returns the appropriate value');
t.equal(binomialCoefficient(5, 3), 10, 'Returns the appropriate value');
t.true(Number.isNaN(binomialCoefficient(NaN, 3)), 'Returns NaN');
t.true(Number.isNaN(binomialCoefficient(5, NaN)), 'Returns NaN');
//t.deepEqual(binomialCoefficient(args..), 'Expected');
//t.equal(binomialCoefficient(args..), 'Expected');
//t.false(binomialCoefficient(args..), 'Expected');
//t.throws(binomialCoefficient(args..), 'Expected');
t.end();
});

View File

@ -1,4 +1,4 @@
Test log for: Wed Feb 14 2018 12:24:07 GMT+0200 (GTB Standard Time)
Test log for: Wed Feb 14 2018 12:33:42 GMT+0200 (GTB Standard Time)
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
> tape test/**/*.test.js | tap-spec
@ -86,6 +86,15 @@ Test log for: Wed Feb 14 2018 12:24:07 GMT+0200 (GTB Standard Time)
√ bindKey is a Function
√ Binds function to an object context
Testing binomialCoefficient
√ binomialCoefficient is a Function
√ Returns the appropriate value
√ Returns the appropriate value
√ Returns the appropriate value
√ Returns NaN
√ Returns NaN
Testing bottomVisible
√ bottomVisible is a Function
@ -1830,8 +1839,8 @@ Test log for: Wed Feb 14 2018 12:24:07 GMT+0200 (GTB Standard Time)
√ Works with multiple promises
total: 913
passing: 913
total: 919
passing: 919
duration: 2.4s