Factorial [阶乘]
# 实现
# JavaScript
/**
* @function Factorial
* @description function to find factorial using recursion.
* @param {Integer} n - The input integer
* @return {Integer} - Factorial of n.
* @see [Factorial](https://en.wikipedia.org/wiki/Factorial)
* @example 5! = 1*2*3*4*5 = 120
* @example 2! = 1*2 = 2
*/
const factorial = (n) => {
if (n === 0) {
return 1
}
return n * factorial(n - 1)
}
export { factorial }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 参考
编辑 (opens new window)
上次更新: 2022/06/20, 20:15:04