Fe-interview: [js] 第50天 请写出一个函数求出N的阶乘(即N!)

Created on 4 Jun 2019  ·  18Comments  ·  Source: haizlin/fe-interview

第50天 请写出一个函数求出N的阶乘(即N!)

js

Most helpful comment

function factorial(n) {
      if (n > 1)  return n*factorial(n-1);
      return 1;
}

All 18 comments

function factorial(n) {
      if (n > 1)  return n*factorial(n-1);
      return 1;
}

function getTotal(n){
  let s = 1,total;
  while(s<=n){
    total = 1*s;
    s++;
  }
  return total
}
const stepNum = (num) => (num === 1 ? num : num * stepNum(num - 1));

console.log(stepNum(3))
console.log(stepNum(4))
console.log(stepNum(5))
console.log(stepNum(6))
const getN = function (n,sum =1) {
   if(typeof n !=='number'){
       return '请输入数字类型'
   }
   if(n === 0){
       return sum;
   }else{
       sum = sum * n;
       return getN( --n ,sum);
   }
}
const getN = function (n,sum =1) {
   if(typeof n !=='number'){
       return '请输入数字类型'
   }
   if(n === 0){
       return sum;
   }else{
       sum = sum * n;
       return getN( --n ,sum);
   }
}

推荐这种写法,进行了尾递归优化,数值很大也不会出现内存溢出的问题

const getFactorial = n => {
    return new Array(n-1).join(',').split(',').reduce( total => { --n;  return total*n}, n)
}
function getN(num) {
  return Array.from(new Array(num)).reduce((cacheNum, it, index) => {
    return cacheNum * (index + 1);
  }, 1);
}
console.log(getN(6));
const factorial = num => {
  if (num > 1) 
    return num * factorial(num - 1)
  return 1
}

console.log(factorial(5))
function getTotal(n){
  let s = 1,total;
  while(s<=n){
    total = 1*s;
    s++;
  }
  return total
}

while( n>=1 ){
...
n--;
}
可以少个变量,感觉比递归方式,这样写舒服点。

这好像都没考虑位数限制

判断了下如果小于0以及其他因素。

    function factorial(num) {
        let data = num;
        if (num < 0) return -1;
        if (num === 0 || num === 1) return 1;
        while (num > 1) {
            num--;
            data *= num;
        }
        return data;
    }

    console.log(factorial(5));  // 120

function getNum (num) {
let s = 1, total = 1;
while (s <= num) {
total = total * s
s ++
}
return total
}
console.log(getNum(5))

function factorial(num, sum = 1) {
    if(num <= 1)return sum;
    sum *= num;
    return factorial(--num, sum);
}

正常递归会爆栈,所以要动态规划
var trailingZeroes = function(n) {
let arr=[1];
for(let i=1;i arr[i]=arr[i-1]*(i+1)
}
return arr[n-1]

};

function tailDiGui(n, r) {
    if (n === 1) {
        return r;
    } else {
        return tailDiGui(n - 1, r * n)
    }
}

function jieCheng(n) {
    return tailDiGui(n, 1)
}
console.log(jieCheng(100));
function factorial(n) {
    if (isFinite(n) && n > 0 && Math.round(n)) {
        if (!(n in factorial)) {
            factorial[n] = n * factorial(n - 1)
        }

        return factorial[n]
    } else {
        return NaN;
    }
}

factorial[1] = 1;

console.log(factorial(1000));
function factorial(n) {
      if (n > 1)  return n*factorial(n-1);
      return 1;
}
function getTotal(n){
  let s = 1,total;
  while(s<=n){
    total = 1*s;
    s++;
  }
  return total
}

上面这个不行,
function getTotal(n){
let s = 1,total=1;
while(s<=n){
total = total*s;
s++;
}
return total
}

Was this page helpful?
0 / 5 - 0 ratings