2022-09-08 10:14:33 +05:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-02-07 08:50:28 -08:00
|
|
|
* @description -> this function returns binomial coefficient
|
2022-09-08 10:14:33 +05:30
|
|
|
* 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) => {
|
2023-10-03 23:08:19 +02:00
|
|
|
if (typeof n !== 'number' || typeof k !== 'number') {
|
2022-09-08 10:14:33 +05:30
|
|
|
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)
|
|
|
|
|
}
|