diff --git a/Maths/BinomialCoefficient.js b/Maths/BinomialCoefficient.js index 65c52dd15d..2bc95cad54 100644 --- a/Maths/BinomialCoefficient.js +++ b/Maths/BinomialCoefficient.js @@ -1,31 +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 binomial coefficient - * of two numbers n & k given by n!/((n-k)!k!) - * @param {number} n - * @param {number} k - * @returns {number} + * @function binomialCoefficient + * @description Calculates the binomial coefficient C(n, k), also known + * as "n choose k". It represents the number of ways to choose k + * items from n items without regard to order. + * Formula: C(n,k) = n! / (k! * (n-k)!) + * @param {number} n - Total number of items + * @param {number} k - Number of items to choose + * @returns {number} - The binomial coefficient C(n, k) + * @see https://en.wikipedia.org/wiki/Binomial_coefficient + * @example binomialCoefficient(5, 2) => 10 + * @example binomialCoefficient(10, 3) => 120 + * @example binomialCoefficient(5, 0) => 1 */ - -import { calcFactorial } from './Factorial' - -export const findBinomialCoefficient = (n, k) => { - if (typeof n !== 'number' || typeof k !== 'number') { - throw Error('Type of arguments must be number.') +const binomialCoefficient = (n, k) => { + if (!Number.isInteger(n) || !Number.isInteger(k)) { + throw new TypeError('Both inputs must be integers') } if (n < 0 || k < 0) { - throw Error('Arguments must be greater than zero.') + throw new TypeError('Both inputs must be non-negative integers') } - let product = 1 - for (let i = n; i > k; i--) { - product *= i + if (k > n) return 0 + if (k === 0 || k === n) return 1 + let result = 1 + for (let i = 0; i < k; i++) { + result = (result * (n - i)) / (i + 1) } - return product / calcFactorial(n - k) + return Math.round(result) } + +export { binomialCoefficient } diff --git a/Maths/test/BinomialCoefficient.test.js b/Maths/test/BinomialCoefficient.test.js index 26960b6ab4..2ea0da63da 100644 --- a/Maths/test/BinomialCoefficient.test.js +++ b/Maths/test/BinomialCoefficient.test.js @@ -1,37 +1,51 @@ -import { findBinomialCoefficient } from '../BinomialCoefficient.js' +import { binomialCoefficient } from '../BinomialCoefficient.js' -describe('Testing findBinomialCoefficient function', () => { - it('should return 56', () => { - const binomialCoefficient = findBinomialCoefficient(8, 3) - expect(binomialCoefficient).toBe(56) +describe('binomialCoefficient', () => { + test('binomialCoefficient(5, 0) returns 1', () => { + expect(binomialCoefficient(5, 0)).toBe(1) }) - it('should return 10', () => { - const binomialCoefficient = findBinomialCoefficient(5, 2) - expect(binomialCoefficient).toBe(10) + test('binomialCoefficient(5, 5) returns 1', () => { + expect(binomialCoefficient(5, 5)).toBe(1) }) - it('should throw error when supplied arguments other than number', () => { - expect(() => { - findBinomialCoefficient('eight', 'three') - }).toThrow(Error) + test('binomialCoefficient(5, 1) returns 5', () => { + expect(binomialCoefficient(5, 1)).toBe(5) }) - it('should throw error when n is less than zero', () => { - expect(() => { - findBinomialCoefficient(-1, 3) - }).toThrow(Error) + test('binomialCoefficient(5, 2) returns 10', () => { + expect(binomialCoefficient(5, 2)).toBe(10) }) - it('should throw error when k is less than zero', () => { - expect(() => { - findBinomialCoefficient(1, -3) - }).toThrow(Error) + test('binomialCoefficient(10, 3) returns 120', () => { + expect(binomialCoefficient(10, 3)).toBe(120) }) - it('should throw error when n and k are less than zero', () => { - expect(() => { - findBinomialCoefficient(-1, -3) - }).toThrow(Error) + test('binomialCoefficient(6, 3) returns 20', () => { + expect(binomialCoefficient(6, 3)).toBe(20) + }) + + test('binomialCoefficient(k > n) returns 0', () => { + expect(binomialCoefficient(3, 5)).toBe(0) + }) + + test('binomialCoefficient(0, 0) returns 1', () => { + expect(binomialCoefficient(0, 0)).toBe(1) + }) + + test('binomialCoefficient(10, 10) returns 1', () => { + expect(binomialCoefficient(10, 10)).toBe(1) + }) + + test('throws TypeError for negative n', () => { + expect(() => binomialCoefficient(-5, 2)).toThrow(TypeError) + }) + + test('throws TypeError for negative k', () => { + expect(() => binomialCoefficient(5, -2)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => binomialCoefficient(5.5, 2)).toThrow(TypeError) }) })