Home > Web Front-end > JS Tutorial > body text

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

王林
Release: 2023-09-03 15:33:07
forward
863 people have browsed it

如何使用 JavaScript 求两个整数的二项式系数?

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

What is the binomial coefficient?

Binomial coefficient refers to the positive integer that appears as a coefficient in the binomial theorem.

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

The binomial coefficients of two numbers n and k represent the number of combinations of r items that can be selected from a set of n items.

For example, we can say that if you want to select 3 balls from a set of 5 balls, the number of ways to do this would be C(5,2).

The formula for the binomial coefficient of C(n,k) is -

C(n,k) = n!/(n-k)!*k!
Copy after login

Find binomial coefficients in JavaScript

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

step

We need to follow certain steps to write code to calculate the binomial coefficient of two integers n and k.

Step 1 - Create a function that accepts two parameters n and k, which will be further used to evaluate the binomial coefficients.

Step 2 - Now we check if both parameters are numbers using Number.isNaN() method.

Step 3 - Now we create an if loop which has two conditions depending on the value of the integer k, the first condition is if k is greater than 0 and the other condition is the value of k A value less than the integer n. If either condition is true, the function returns the value zero.

Step 4 - Now we create an if loop again with two other conditions depending on the value of the integer k, the first condition is if k is equal to 1 and the other conditions are of k The value is equal to the value integer n. If either condition is true, the function returns the value 1.

Step 5 - Now we create a final if loop with two conditions depending on the value of the integer k, the first condition is if k is equal to 1 and the other condition is The value of k is equal to value n-1. If either condition is true, the function returns the value n.

Step 6 - Here we will write logic to find the binomial coefficients of two integers n and k. To find the binomial coefficients we need to create a for loop starting with j = 2 until the condition j<=k,每次循环运行时,变量结果的值都会更新并与变量结果本身相乘。

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

Step 8 - In the final step we will provide the values ​​of the two integers n and k from which we want to evaluate the binomial coefficients.

Example

We can use the following HTML code to calculate 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>
Copy after login

Throughout the scenario, we must understand that to find the binomial coefficients of two integers n and k, we need to first check some conditions given above and then apply logic to calculate the binomial coefficients.

The above is the detailed content of How to find the binomial coefficients of two integers using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template