From 1ebc1af1aed88b4b868da9c71e8a9562cf33a4fc Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Wed, 7 Sep 2022 15:28:51 +0530 Subject: [PATCH 1/4] feat: Add binomial coefficient implementation --- Maths/BinomialCoefficient.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Maths/BinomialCoefficient.js diff --git a/Maths/BinomialCoefficient.js b/Maths/BinomialCoefficient.js new file mode 100644 index 0000000000..d8d4eef2e9 --- /dev/null +++ b/Maths/BinomialCoefficient.js @@ -0,0 +1,31 @@ +/* + * Author: Akshay Dubey (https://github.com/itsAkshayDubey) + * Binomial Coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient + * function to find binomial coefficient of numbers n and k. + * return binomial coefficient of n,k + */ + +/** + * @function findBinomialCoefficient + * @description -> this function returns bonimial coefficient + * of two numbers n & k given by n!/((n-k)!k!) + * @param {number} n + * @param {number} k + * @returns {number} + */ + +import { calcFactorial } from './Factorial' + +export const findBinomialCoefficient = (n,k) => { + if ((typeof n !== 'number') || (typeof k !== 'number')) { + throw Error('Type of arguments must be number.') + } + if (n <= 0 || k <= 0) { + throw Error('Arguments must be greater than zero.') + } + let product = 1 + for (let i = n; i > k; i--) { + product *= i + } + return product / calcFactorial(n - k) +} From 31a408b12800b68a9d61516f2a669d45d5b31871 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Wed, 7 Sep 2022 15:29:16 +0530 Subject: [PATCH 2/4] test: Add tests for binomial coefficient --- Maths/test/BinomialCoefficient.test.js | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Maths/test/BinomialCoefficient.test.js diff --git a/Maths/test/BinomialCoefficient.test.js b/Maths/test/BinomialCoefficient.test.js new file mode 100644 index 0000000000..3f5fef21b1 --- /dev/null +++ b/Maths/test/BinomialCoefficient.test.js @@ -0,0 +1,29 @@ +import { findBinomialCoefficient } from '../BinomialCoefficient.js' + +describe('Testing findBinomialCoefficient function', () => { + it('should return 56', () => { + const binomialCoefficient = findBinomialCoefficient(8,3) + expect(binomialCoefficient).toBe(56) + }) + + it('should return 10', () => { + const binomialCoefficient = findBinomialCoefficient(5,2) + expect(binomialCoefficient).toBe(10) + }) + + it('should throw error when supplied arguments other than number', () => { + expect(() => { findBinomialCoefficient('eight','three') }).toThrow(Error) + }) + + it('should throw error when n is less than zero', () => { + expect(() => { findBinomialCoefficient(-1,3) }).toThrow(Error) + }) + + it('should throw error when k is less than zero', () => { + expect(() => { findBinomialCoefficient(1,-3) }).toThrow(Error) + }) + + it('should throw error when n and k are less than zero', () => { + expect(() => { findBinomialCoefficient(-1,-3) }).toThrow(Error) + }) +}) From 3524bc70741cccb5e44305ec3cd45f31c3c9ca18 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Wed, 7 Sep 2022 15:34:11 +0530 Subject: [PATCH 3/4] Minor code style fixes --- Maths/BinomialCoefficient.js | 2 +- Maths/test/BinomialCoefficient.test.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Maths/BinomialCoefficient.js b/Maths/BinomialCoefficient.js index d8d4eef2e9..54ed205d83 100644 --- a/Maths/BinomialCoefficient.js +++ b/Maths/BinomialCoefficient.js @@ -16,7 +16,7 @@ import { calcFactorial } from './Factorial' -export const findBinomialCoefficient = (n,k) => { +export const findBinomialCoefficient = (n, k) => { if ((typeof n !== 'number') || (typeof k !== 'number')) { throw Error('Type of arguments must be number.') } diff --git a/Maths/test/BinomialCoefficient.test.js b/Maths/test/BinomialCoefficient.test.js index 3f5fef21b1..0ac46ab1c2 100644 --- a/Maths/test/BinomialCoefficient.test.js +++ b/Maths/test/BinomialCoefficient.test.js @@ -2,28 +2,28 @@ import { findBinomialCoefficient } from '../BinomialCoefficient.js' describe('Testing findBinomialCoefficient function', () => { it('should return 56', () => { - const binomialCoefficient = findBinomialCoefficient(8,3) + const binomialCoefficient = findBinomialCoefficient(8, 3) expect(binomialCoefficient).toBe(56) }) it('should return 10', () => { - const binomialCoefficient = findBinomialCoefficient(5,2) + const binomialCoefficient = findBinomialCoefficient(5, 2) expect(binomialCoefficient).toBe(10) }) it('should throw error when supplied arguments other than number', () => { - expect(() => { findBinomialCoefficient('eight','three') }).toThrow(Error) + expect(() => { findBinomialCoefficient('eight', 'three') }).toThrow(Error) }) it('should throw error when n is less than zero', () => { - expect(() => { findBinomialCoefficient(-1,3) }).toThrow(Error) + expect(() => { findBinomialCoefficient(-1, 3) }).toThrow(Error) }) it('should throw error when k is less than zero', () => { - expect(() => { findBinomialCoefficient(1,-3) }).toThrow(Error) + expect(() => { findBinomialCoefficient(1, -3) }).toThrow(Error) }) it('should throw error when n and k are less than zero', () => { - expect(() => { findBinomialCoefficient(-1,-3) }).toThrow(Error) + expect(() => { findBinomialCoefficient(-1, -3) }).toThrow(Error) }) }) From b3c013aa9dc6885b20721cd3c11f239237c8b6e0 Mon Sep 17 00:00:00 2001 From: Akshay Dubey Date: Wed, 7 Sep 2022 16:40:05 +0530 Subject: [PATCH 4/4] fix: Fix zero checks for n and k --- Maths/BinomialCoefficient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/BinomialCoefficient.js b/Maths/BinomialCoefficient.js index 54ed205d83..f445c8fb61 100644 --- a/Maths/BinomialCoefficient.js +++ b/Maths/BinomialCoefficient.js @@ -20,7 +20,7 @@ export const findBinomialCoefficient = (n, k) => { if ((typeof n !== 'number') || (typeof k !== 'number')) { throw Error('Type of arguments must be number.') } - if (n <= 0 || k <= 0) { + if (n < 0 || k < 0) { throw Error('Arguments must be greater than zero.') } let product = 1