How to find the binomial coefficient of two integers using JavaScript?


In this tutorial, we are going to learn how to find the binomial coefficient of two integers using JavaScript. Before learning it we should know what binomial coefficient is and what it refers to.

What are Binomial Coefficients?

Binomial coefficients refer to the positive integers that occur as coefficients in the binomial theorem.

A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n.

A binomial coefficient of two numbers n and k signifies the number of combinations of r items that can be selected from a set of n items.

For example we can say if you want to choose 3 balls from a set of 5 balls the number of ways to do it will be C(5,2).

Formula of the Binomial Coefficient of C(n,k) is given as −

C(n,k) = n!/(n-k)!*k!

Finding Binomial Coefficients in JavaScript

Now we are going to learn how we can find binomial coefficients using JavaScript. Here we will provide two variables n and k and using code we will calculate the binomial coefficient using some conditions provided.

Steps

There are certain steps we need to follow to write the code to evaluate the binomial coefficient of two integers n and k.

Step 1 − Create a function that takes two arguments n and k which will be further used to evaluate the binomial coefficient.

Step 2 − Now we check whether both the arguments are numbers or not by using the Number.isNaN() method.

Step 3 − Now we create an if loop with two conditions depending on the value of integer k first condition is if k is greater than 0 or not and other if the value of k is less than value of integer n. If any of the one condition is true than the function will return value zero.

Step 4 − Now we again create an if loop with other two conditions depending on the value of integer k first condition is if k is equal to 1and other if the value of k is equal to value of integer n. If any of the one condition is true than the function will return value one.

Step 5 − Now we create a last if loop with two conditions depending on the value of integer k first condition is if k is equal to 1 and other if the value of k is equal to value of n-1. If any of the one condition is true than the function will return value n.

Step 6 − In this we are going to write the logic to find the binomial coefficient of two integers n and k. To find the binomial coefficient we need to create a for loop initiating with j = 2 and up to condition j<=k and every time the loop runs the value of the variable result is updated and gets multiplied with the variable result itself.

Step 7 − After evaluating the value of integer result we will use the Math.round() function to find the round-off of the result.

Step 8 − At this last step we will provide the value of the two integers n and k over which we want to evaluate the binomial coefficient.

Example

We can use the below HTML code to evaluate the binomial coefficient of two integers n and k using JavaScript

<!DOCTYPE html> <html> <head> <h2> Tutorials Point </h2> </head> <body> <script> function Calculate (n, k){ if(Number.isNaN (n) || Number.isNaN (k)){ return NaN; } if(k < 0 || k > n){ return 0 } if(k === 0 || k === n){ return 1 } if(k === 1 || k === n - 1){ return n } let result = n; for(let j = 2; j <= k; j++){ result *= (n - j + 1) / j; } return Math.round(result); } document.write("Binomial Coefficient of 15 and 6 is : " + Calculate(15, 6)) </script> </body> </html>

In this whole scenario we got to understand that to find the binomial coefficient of two integers n and k we need to firstly check some condition given above and then apply logic to calculate the binomial coefficient.

Updated on: 18-Oct-2022

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements