diff --git a/snippets/binomialCoefficient.md b/snippets/binomialCoefficient.md new file mode 100644 index 000000000..d5ff87d16 --- /dev/null +++ b/snippets/binomialCoefficient.md @@ -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 +``` diff --git a/tag_database b/tag_database index b608bd0c8..cad9870dc 100644 --- a/tag_database +++ b/tag_database @@ -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 diff --git a/test/binomialCoefficient/binomialCoefficient.js b/test/binomialCoefficient/binomialCoefficient.js new file mode 100644 index 000000000..a9808585d --- /dev/null +++ b/test/binomialCoefficient/binomialCoefficient.js @@ -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; \ No newline at end of file diff --git a/test/binomialCoefficient/binomialCoefficient.test.js b/test/binomialCoefficient/binomialCoefficient.test.js new file mode 100644 index 000000000..2f0e8ab20 --- /dev/null +++ b/test/binomialCoefficient/binomialCoefficient.test.js @@ -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(); +}); diff --git a/test/testlog b/test/testlog index f4af90680..ab29b55f5 100644 --- a/test/testlog +++ b/test/testlog @@ -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