Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Binomial coefficient implementation#1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletionsMaths/BinomialCoefficient.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +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 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)
}
29 changes: 29 additions & 0 deletionsMaths/test/BinomialCoefficient.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
import { findBinomialCoefficient } from '../BinomialCoefficient.js'

describe('Testing findBinomialCoefficient function', () => {
it('should return 56', () => {
const binomialCoefficient = findBinomialCoefficient(8, 3)
expect(binomialCoefficient).toBe(56)
})

it('should return 10', () => {
const binomialCoefficient = findBinomialCoefficient(5, 2)
expect(binomialCoefficient).toBe(10)
})

it('should throw error when supplied arguments other than number', () => {
expect(() => { findBinomialCoefficient('eight', 'three') }).toThrow(Error)
})

it('should throw error when n is less than zero', () => {
expect(() => { findBinomialCoefficient(-1, 3) }).toThrow(Error)
})

it('should throw error when k is less than zero', () => {
expect(() => { findBinomialCoefficient(1, -3) }).toThrow(Error)
})

it('should throw error when n and k are less than zero', () => {
expect(() => { findBinomialCoefficient(-1, -3) }).toThrow(Error)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp