Javascript finds factorial n

PHPz
Release: 2023-05-09 14:42:37
Original
1018 people have browsed it

JavaScript is a programming language widely used in web development. It is a very powerful tool capable of handling various functions such as dynamic web pages, form validation, responding to user input, and data-driven applications.

In mathematical calculations, factorial (n!) refers to the result of multiplying positive integers n, for example, n!=n(n-1)(n-2)...1. In JavaScript, we can use for loops and recursive functions to calculate factorials. Below I will introduce how to use JavaScript to find factorial n.

Using for loop

We can use for loop to calculate factorial n, the code is as follows:

function factorial(num) {
  var result = 1;
  for(var i=num; i>=1; i--){
    result *= i;
  }
  return result;
}
Copy after login

This function uses a variable result to store The result of the calculation. It loops through all integers from num to 1 and multiplies them by result. Finally return result.

Use recursive function

Another method is to use a recursive function to calculate the factorial n. The code is as follows:

function factorial(num) {
  if(num<=1){
    return 1;
  } else{
    return num * factorial(num-1);
  }
}
Copy after login

This function uses a recursive call to calculate the factorial . If the number passed in is less than or equal to 1, the result is 1. Otherwise, the function itself is called recursively to calculate the factorial of (num-1), and the final result is num*(num-1)!.

Use BigInt to handle large number calculations

In JavaScript, using the ordinary Number type may encounter accuracy problems when calculating large numbers. Therefore, we can use BigInt to handle large number calculations. The code is as follows:

function factorial(num) {
  if (num < 0) return; 
  if (num <= 1) return 1n; 
  let result = 1n; 
  for (let i = 2n; i <= BigInt(num); i++) { 
    result *= i; 
  } 
  return result; 
}
Copy after login

In the code, 1n is used to replace 1, 2n is used to replace 2, and BigInt(num) is used to convert num to BigInt type for calculation.

Summary

Factorial is a concept often used in mathematical calculations, and JavaScript provides a variety of methods for calculating factorial. The above introduces the method of calculating factorial using for loop, recursive function and BigInt. Developers can choose a method that suits them according to their needs. I hope these codes can help everyone better understand and use JavaScript.

The above is the detailed content of Javascript finds factorial n. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!