How to find the factorial of 13 in javascript

青灯夜游
Release: 2022-11-15 18:22:21
Original
2361 people have browsed it

Method: 1. Use for loop, syntax "var cj=1;for(var i=1;i1?n*fac(n-1):1}".

How to find the factorial of 13 in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

Factorial is an arithmetic symbol invented by Christian Kramp (1760-1826) in 1808. It is a mathematical term.

The factorial of a positive integer is the product of all positive integers less than and equal to the number, and the factorial of 0 is 1. The factorial of a natural number n is written n!. In 1808, Christian Carman introduced this notation.

That is, n!=1×2×3×...×(n-1)×n. Factorial can also be defined recursively: 0!=1, n!=(n-1)!×n.

Javascript supports multiple methods to implement factorial, let’s take a look.

Method 1: Use for loop to achieve

If you want to find the factorial of 13, you need to traverse the numbers 1~13, so the initial condition of the for loop can be set to i = 1, the restriction can be i

for (var i = 1; i <= 13; i++) {
}
Copy after login

Then in the loop body "{}", multiply the i values ​​of each loop. This requires an intermediate quantity cj to store the product. The initial value of the variable cj must be 1, so as not to affect the result. There are two ways to write it (just choose one):

cj *= i;
//或
cj = cj * i;
Copy after login

After the loop ends, the value of variable cj will be the factorial of 13, and then output it.

The complete implementation code is given below:

var cj = 1;
for (var i = 1; i <= 13; i++) {
    cj *= i;
}
console.log( "13的阶乘为: " + cj);
Copy after login

How to find the factorial of 13 in javascript

Method 2: Use recursive functions to implement

function fac(num) {
    if (num <= 1) {
        return 1;
    } else {
        // 闭包 
        return num * fac(--num); //方法一 
        //return num*fac(num-1); //方法二 
        // return num*arguments.callee(num-1);//方法三 
        // return num*arguments.callee(--num);//方法四 
        /*但在严格模式下不能通过脚本访问arguments.callee*/
    }
}
var result = fac(13);
console.log( "13的阶乘为: " + result);
Copy after login

How to find the factorial of 13 in javascript

Method 3: Using the ternary operator

function fac(n) {
    return n > 1 ? n * fac(n - 1) : 1
}
console.log(&#39;2的阶乘为:&#39;, fac(2))
console.log(&#39;3的阶乘为:&#39;, fac(3))
console.log(&#39;4的阶乘为:&#39;, fac(4))
console.log(&#39;13的阶乘为:&#39;, fac(13))
Copy after login

How to find the factorial of 13 in javascript

[Recommended learning: javascript Advanced Tutorial

The above is the detailed content of How to find the factorial of 13 in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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