factorial calls it factorial, as Wikipedia describes the " factorial of a positive integer is the product of all positive integers less than and equal to that number, and has a 0
factorial of 1
." n
the factorial writing n!
of natural numbers . ”
The factorial function is a typical example of a recursive (Haskell) function. Recursive functions may be used in JavaScript. In practice, however, you may not consider when and where recursion is useful, or if the use is not good, it can lead to many problems.
In this article we look at how JavaScript implements the factorial function.
factorial function
Let's take a simple look at the mathematical factorial function, and at the beginning it says that the argument of the factorial function is a natural number that returns the 1
product of the number of the numbers. For example, 6
the factorial is 1 × 2 × 3 × 4 × 5 × 6 = 720
, and in this way, it can be represented by a recursive function. If n
Yes 6
, its pattern is:
0! = 11! = 12! = 2 * 13! = 3 * 2 * 14! = 4 * 3 * 2 * 15! = 5 * 4 * 3 * 2 * 16! = 6 * 5 * 4 * 3 * 2 * 1
In this way, factorial can be simply defined as:
0
The factorial is1
- The factorial of any number is the factorial of this number multiplied by the number of entering primary one of this number.
You can define a factorial()
function to represent:
factorial (0) = 1factorial (n) = n * factorial (n - 1)
The first line represents 0
the factorial 1
, and the second line represents the factorial of any other number n
equal to n
n-1
the multiplier. Note the n-1
enclosed bracket: Without the parentheses the code is parsed (factorial n) - 1
, and the function behaves in a high priority and is executed first .
Factorial implementation algorithm
The factorial function is also a typical algorithm in javascript:
factorial()
The function returns the factorial of an integer
- If the integer
n
is expressed in letters, the factorial of all positive integers is less than or equal ton
- The representation symbol for the factorial universal is
n!
Factorial function test Case
factorial(0)
Return1
factorial(5)
Return120
factorial(10)
Return3628800
factorial(20)
Return2432902008176640000
Factorial Implementation method
There are several scenarios for JavaScript to implement factorial, and then there are several ways to do it.
Method 1
function Factorial(num) {If num is an integer less than 0, it rejects theif (Num <0) {Return-1; } esleif (num = = =0 | | num = =1) {If NUM is a 0 or 1, its factorial is 1return 1;} else { //Call recursive return (num * factorial (num- 1)); / * First: Do not call only once, there will be more than one nested * per call num = = =? Num * Factorial (num-1) * 1st factorial (5) 5 * factorial (5-1) = Fact Orial (4) * 2nd factorial (4) 4 * factorial (4-1) + factorial (3) * 3rd factorial (3) 3 * factorial (3-1) = Factoria L (2) * 4th factorial (2) 2 * factorial (2-1) = factorial (1) * 5th factorial (1) 1 * factorial (1-1) + factorial (0) * Part II: If the conditions are met, the number num itself is multiplied by 1 = num * 1, * and the function will return the Total product * 5th = 5 * (5-1) = num = 5 * 4 = * 4th = 20 * (4- 1) = num = 120 * 3 = 3rd * (3-1) = num = * 2 = 2nd * + * (2-1) 1 = 1st * + = + = 120 * Write the above procedure as one line: (5* (5-1) * (4-1) * (3-1) * (2-1)) =5*4*3*2*1=120 * *}}
Remove Comments:
function factorial (num) { if (num < 0) { return -1; } else if (num === 0 || num === 1) { return 1; } else { return (num * factorial(num - 1)); }}
Method 2
Method 1 takes a recursive function and then looks at a while
cyclic scheme.
function Factorial(num) {First step: Create a variable result to store num resultsvar result = num;//if num is less than 0, if (num < 0) {return-1;} else if (num = = 0 | | num = = 1) {//if num = 0 or num = 1,factorial will return 1 return 1;} else {//create a while loop while (num > 1) {//each iteration of Num-minus 1 num--; result *= num; /* * number of iterations num num--result result *= num * 1st 5 4 5 = 5 * 4 * 2nd 4 3 * = * 3 * 3rd 3 2 6 0 * 2 * 4th 2 1 [+] * 1 * 5th 1 0 120 * END traversal */}} //return factorial final product return result;}
Remove Comments:
function factorial (num) { var result = num; if (num < 0) { return -1; } else if (num === 0 || num === 1) { return 1; } else { while (num > 1) { num--; result *= num; } } return result;}
Method 3
In addition to looping through loops while
, you can also use for
loop traversal:
function Factorial(num) {If NUM is less than 0,factorial will terminateif (num < 0) {return-1;} span class= "keyword" >else if (num = = 0 | | num = = 1) { span class= "comment" >//if num = 0 or num = 1,factorial will return 1 return 1;} else {//start for loop, each traversal minus 1 for (var i = Num-1; I >= 1; i--) {num *= i; /* * traversal count num i = num-1 num *= i i--i>= 1? * 1st 5 4 = 5-1 = 5 * 4 3 Yes * 2nd 20 3 = 4- 1 = 3 2 Yes * 3rd 2 = 3-1 = * 2 1 Yes * 4th 1 = 2-1 = * 1 0 No * 5th 120 0 120 * End for Follow Ring */}} return num;}
Remove Comments:
function factorial (num) { if (num < 0) { return -1; } else if (num === 0 || num === 1) { return 1; } else { for (var i = num - 1; i >= 1; i--) { num *= i; } } return num;}
JavaScript algorithm exercise: factorial (factorial) function