Skip to content

[js] 第50天 请写出一个函数求出N的阶乘(即N!) #191

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

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

Activity

cleverboy32

cleverboy32 commented on Jun 5, 2019

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

myprelude

myprelude commented on Jun 13, 2019

@myprelude
function getTotal(n){
  let s = 1,total;
  while(s<=n){
    total = 1*s;
    s++;
  }
  return total
}
Konata9

Konata9 commented on Jul 9, 2019

@Konata9
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))
wyx2014

wyx2014 commented on Jul 25, 2019

@wyx2014
const getN = function (n,sum =1) {
   if(typeof n !=='number'){
       return '请输入数字类型'
   }
   if(n === 0){
       return sum;
   }else{
       sum = sum * n;
       return getN( --n ,sum);
   }
}
DingkeXue

DingkeXue commented on Aug 14, 2019

@DingkeXue
const getN = function (n,sum =1) {
   if(typeof n !=='number'){
       return '请输入数字类型'
   }
   if(n === 0){
       return sum;
   }else{
       sum = sum * n;
       return getN( --n ,sum);
   }
}

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

damofan

damofan commented on Sep 7, 2019

@damofan
const getFactorial = n => {
	return new Array(n-1).join(',').split(',').reduce( total => { --n;  return total*n}, n)
}
JJL-SH

JJL-SH commented on Sep 24, 2019

@JJL-SH
function getN(num) {
  return Array.from(new Array(num)).reduce((cacheNum, it, index) => {
    return cacheNum * (index + 1);
  }, 1);
}
console.log(getN(6));
ZindexYG

ZindexYG commented on Oct 18, 2019

@ZindexYG
const factorial = num => {
  if (num > 1) 
    return num * factorial(num - 1)
  return 1
}

console.log(factorial(5))
huangd-d

huangd-d commented on Mar 30, 2020

@huangd-d
function getTotal(n){
  let s = 1,total;
  while(s<=n){
    total = 1*s;
    s++;
  }
  return total
}

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

xiaocaicaiaichicai

xiaocaicaiaichicai commented on Apr 15, 2020

@xiaocaicaiaichicai

这好像都没考虑位数限制

lizhesystem

lizhesystem commented on May 9, 2020

@lizhesystem

判断了下如果小于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
c37csq

c37csq commented on Jun 2, 2020

@c37csq

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

adi0754

adi0754 commented on Jun 16, 2020

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

waterkitten commented on Aug 9, 2020

@waterkitten

正常递归会爆栈,所以要动态规划
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

bozaigao commented on Oct 8, 2020

@bozaigao
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));
bozaigao

bozaigao commented on Oct 17, 2020

@bozaigao
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));
smile-2008

smile-2008 commented on Nov 11, 2020

@smile-2008
function factorial(n) {
      if (n > 1)  return n*factorial(n-1);
      return 1;
}
jamsehua

jamsehua commented on Jan 11, 2021

@jamsehua
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
}

xiaoqiangz

xiaoqiangz commented on Jun 6, 2022

@xiaoqiangz

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

panpanxuebing

panpanxuebing commented on Dec 12, 2024

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

factorial(0) // 1
factorial(5) // 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@JJL-SH@Konata9@wyx2014

        Issue actions

          [js] 第50天 请写出一个函数求出N的阶乘(即N!) · Issue #191 · haizlin/fe-interview