forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinomial_coefficient.ts
More file actions
24 lines (22 loc) · 870 Bytes
/
binomial_coefficient.ts
File metadata and controls
24 lines (22 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { factorial } from './factorial'
/**
* @function binomialCoefficient
* @description Calculate the binomial coefficient (n choose k) of two input numbers.
* @param {number} n - the total number of items
* @param {number} k - the number of items to be chosen
* @return {number} - Binomial coefficient (n choose k)
* @see https://en.wikipedia.org/wiki/Binomial_coefficient
* @example binomialCoefficient(5, 2) = 10
* @example binomialCoefficient(10, 3) = 120
* @example binomialCoefficient(6, 0) = 1
*/
export const binomialCoefficient = (n: number, k: number): number => {
// Check if k is larger than n or negative
if (k > n || k < 0) {
return 0
}
// Calculate the binomial coefficient using the implemented factorial
const numerator = factorial(n)
const denominator = factorial(k) * factorial(n - k)
return numerator / denominator
}