第50天 请写出一个函数求出N的阶乘(即N!)
Activity
cleverboy32 commentedon Jun 5, 2019
myprelude commentedon Jun 13, 2019
Konata9 commentedon Jul 9, 2019
wyx2014 commentedon Jul 25, 2019
DingkeXue commentedon Aug 14, 2019
推荐这种写法,进行了尾递归优化,数值很大也不会出现内存溢出的问题
damofan commentedon Sep 7, 2019
JJL-SH commentedon Sep 24, 2019
ZindexYG commentedon Oct 18, 2019
huangd-d commentedon Mar 30, 2020
while( n>=1 ){
...
n--;
}
可以少个变量,感觉比递归方式,这样写舒服点。
xiaocaicaiaichicai commentedon Apr 15, 2020
这好像都没考虑位数限制
lizhesystem commentedon May 9, 2020
判断了下如果小于0以及其他因素。
c37csq commentedon Jun 2, 2020
function getNum (num) {
let s = 1, total = 1;
while (s <= num) {
total = total * s
s ++
}
return total
}
console.log(getNum(5))
adi0754 commentedon Jun 16, 2020
waterkitten commentedon Aug 9, 2020
正常递归会爆栈,所以要动态规划
var trailingZeroes = function(n) {
let arr=[1];
for(let i=1;i<n;i++){
arr[i]=arr[i-1]*(i+1)
}
return arr[n-1]
};
bozaigao commentedon Oct 8, 2020
bozaigao commentedon Oct 17, 2020
smile-2008 commentedon Nov 11, 2020
jamsehua commentedon Jan 11, 2021
上面这个不行,
function getTotal(n){
let s = 1,total=1;
while(s<=n){
total = total*s;
s++;
}
return total
}
xiaoqiangz commentedon Jun 6, 2022
function factorial(num, sum = 1) {
if (num < 1) {
return sum
}
sum *= num
return factorial(--num, sum)
panpanxuebing commentedon Dec 12, 2024