Using BigInt to calculate long factorials in JavaScript


In the given problem statement we are asked to find out long factorials of a Bigint value with the help of javascript functionalities. BigInt is a data type in javascript, which is used to calculate the factorial of a large number.

What is the BigInt data type in JavaScript ?

Let's understand the working of a BigInt data type in JavaScript.

In Javascript, there are so many data types available to define the data, one of them is BigInt. BigInt is a built−in data type in Javascript which represents integers of large size. It was introduced in ECMAScript 2020. It allows us to work with the integers that are big in number as per the standard numbers in javascript. With the help of BigInt we can do mathematical operations on the integers of any size. So here we will be calculating the large factorials.

As we know that the factorial is the multiplication of all the positive integers up to the given number. Let's understand this by calculating the factorial of 5.

5 = 5 x 4 x 3 x 2 x 1 = 120

In the above example we have calculated the factorial for 5 which is 120. So factorials can become very large very quickly, making them difficult to calculate using javascript data types, so here BigInt comes into the picture.

Understanding the Logic

The problem statement is needed to calculate the factorial of the given big int number. The code takes an int value as input.

In the algorithm we will provide a simple and efficient way to calculate the factorial of a number using the BigInt data type in Javascript. The code will use a for loop to iterate through all the numbers from 2 to the given input number lets say n. It will initialize a BigInt variable result with a value of 1 and then it will multiply each number to get the result. So the final result will be the factorial of number n.

Algorithm

Step 1 − At the beginning, we will start creating a function for calculating the factorial of a given number.

Step 2 − Then in the next step of the algorithm we will be using a for loop to calculate the product of all the numbers from 2 to the given number num.

Step 3 − After the second step, the initial value of the outcome is set to 1n. 1n is a BigInt with a value of 1.

Step 4 − Now forwarding, The loop which was created in step 2 was started with 2n, which is a BigInt value of 2, and it will iterate up to the number num. And multiplying every number in turn to get the result.

Step 5 − Show the output with the help of a factorial variable.

Example

 // define the function to calculate factorial
  function calculateFactorial(n) {
    var fact = 1n;
    for (let i = 2n; i <= n; i++) {
      fact *= i;
    }
    return fact;
  }

  //define the number
  const num = 35;
  const factorial = calculateFactorial(num);

  console.log(`The Factorial of ${num} is "${factorial}"`);

Output

The Factorial of 35 is "10333147966386144929666651337523200000000"

The factorial object is used in the code to hold the resultant factorial for the given number, num 35. The result of the function is the factorial of the input integer.

The Complexity

The time required to execute the aforementioned code and function will be O(n). since doing so necessitates going through each number from 2 to num once. Additionally, because just the most recent value of the result needs to be stored, the space complexity will be O(1). So, for really big values of num, we may state that this approach is quite effective.

Conclusion

This is how we can resolve the issue posed in the above problem statement. The easiest and most accurate way to determine the factorial in JavaScript for any given number. It uses the for loop to iterate the numbers from 2 to n. By using BigInt in JavaScript, we can easily calculate factorials of large numbers. The algorithm for calculating factorials using BigInt is simple, efficient, and has a time complexity of O(n) and a space complexity of O(1).

Updated on: 23-Aug-2023

894 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements