Skip to content

Commit 73f6cc1

Browse files
feat: add binomial coefficient algorithm
1 parent 5c39e87 commit 73f6cc1

2 files changed

Lines changed: 62 additions & 48 deletions

File tree

‎Maths/BinomialCoefficient.js‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
/*
2-
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
3-
* Binomial Coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient
4-
* function to find binomial coefficient of numbers n and k.
5-
* return binomial coefficient of n,k
6-
*/
7-
81
/**
9-
* @function findBinomialCoefficient
10-
* @description -> this function returns binomial coefficient
11-
* of two numbers n & k given by n!/((n-k)!k!)
12-
* @param {number} n
13-
* @param {number} k
14-
* @returns {number}
2+
* @function binomialCoefficient
3+
* @description Calculates the binomial coefficient C(n, k), also known
4+
* as "n choose k". It represents the number of ways to choose k
5+
* items from n items without regard to order.
6+
* Formula: C(n,k) = n! / (k! * (n-k)!)
7+
* @param {number} n - Total number of items
8+
* @param {number} k - Number of items to choose
9+
* @returns {number} - The binomial coefficient C(n, k)
10+
* @see https://en.wikipedia.org/wiki/Binomial_coefficient
11+
* @example binomialCoefficient(5, 2) => 10
12+
* @example binomialCoefficient(10, 3) => 120
13+
* @example binomialCoefficient(5, 0) => 1
1514
*/
16-
17-
import { calcFactorial } from './Factorial'
18-
19-
export const findBinomialCoefficient = (n, k) => {
20-
if (typeof n !== 'number' || typeof k !== 'number') {
21-
throw Error('Type of arguments must be number.')
15+
const binomialCoefficient = (n, k) => {
16+
if (!Number.isInteger(n) || !Number.isInteger(k)) {
17+
throw new TypeError('Both inputs must be integers')
2218
}
2319
if (n < 0 || k < 0) {
24-
throw Error('Arguments must be greater than zero.')
20+
throw new TypeError('Both inputs must be non-negative integers')
2521
}
26-
let product = 1
27-
for (let i = n; i > k; i--) {
28-
product *= i
22+
if (k > n) return 0
23+
if (k === 0 || k === n) return 1
24+
let result = 1
25+
for (let i = 0; i < k; i++) {
26+
result = (result * (n - i)) / (i + 1)
2927
}
30-
return product / calcFactorial(n - k)
28+
return Math.round(result)
3129
}
30+
31+
export { binomialCoefficient }
Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
1-
import { findBinomialCoefficient } from '../BinomialCoefficient.js'
1+
import { binomialCoefficient } from '../BinomialCoefficient.js'
22

3-
describe('Testing findBinomialCoefficient function', () => {
4-
it('should return 56', () => {
5-
const binomialCoefficient = findBinomialCoefficient(8, 3)
6-
expect(binomialCoefficient).toBe(56)
3+
describe('binomialCoefficient', () => {
4+
test('binomialCoefficient(5, 0) returns 1', () => {
5+
expect(binomialCoefficient(5, 0)).toBe(1)
76
})
87

9-
it('should return 10', () => {
10-
const binomialCoefficient = findBinomialCoefficient(5, 2)
11-
expect(binomialCoefficient).toBe(10)
8+
test('binomialCoefficient(5, 5) returns 1', () => {
9+
expect(binomialCoefficient(5, 5)).toBe(1)
1210
})
1311

14-
it('should throw error when supplied arguments other than number', () => {
15-
expect(() => {
16-
findBinomialCoefficient('eight', 'three')
17-
}).toThrow(Error)
12+
test('binomialCoefficient(5, 1) returns 5', () => {
13+
expect(binomialCoefficient(5, 1)).toBe(5)
1814
})
1915

20-
it('should throw error when n is less than zero', () => {
21-
expect(() => {
22-
findBinomialCoefficient(-1, 3)
23-
}).toThrow(Error)
16+
test('binomialCoefficient(5, 2) returns 10', () => {
17+
expect(binomialCoefficient(5, 2)).toBe(10)
2418
})
2519

26-
it('should throw error when k is less than zero', () => {
27-
expect(() => {
28-
findBinomialCoefficient(1, -3)
29-
}).toThrow(Error)
20+
test('binomialCoefficient(10, 3) returns 120', () => {
21+
expect(binomialCoefficient(10, 3)).toBe(120)
3022
})
3123

32-
it('should throw error when n and k are less than zero', () => {
33-
expect(() => {
34-
findBinomialCoefficient(-1, -3)
35-
}).toThrow(Error)
24+
test('binomialCoefficient(6, 3) returns 20', () => {
25+
expect(binomialCoefficient(6, 3)).toBe(20)
26+
})
27+
28+
test('binomialCoefficient(k > n) returns 0', () => {
29+
expect(binomialCoefficient(3, 5)).toBe(0)
30+
})
31+
32+
test('binomialCoefficient(0, 0) returns 1', () => {
33+
expect(binomialCoefficient(0, 0)).toBe(1)
34+
})
35+
36+
test('binomialCoefficient(10, 10) returns 1', () => {
37+
expect(binomialCoefficient(10, 10)).toBe(1)
38+
})
39+
40+
test('throws TypeError for negative n', () => {
41+
expect(() => binomialCoefficient(-5, 2)).toThrow(TypeError)
42+
})
43+
44+
test('throws TypeError for negative k', () => {
45+
expect(() => binomialCoefficient(5, -2)).toThrow(TypeError)
46+
})
47+
48+
test('throws TypeError for float input', () => {
49+
expect(() => binomialCoefficient(5.5, 2)).toThrow(TypeError)
3650
})
3751
})

0 commit comments

Comments
 (0)