mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
32 lines
871 B
JavaScript
32 lines
871 B
JavaScript
/*
|
|
* 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)
|
|
}
|