factorial of a number

前端 未结 28 2507
滥情空心
滥情空心 2020-12-09 11:57

I have the following code but it is not giving perfect result for factorial can u find it out plz



        
28条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 12:32

    I am not sure why no one used dynamic programming to answer this, it's by far the most efficient way to build something on a factorial in my view.

     var mem = [];
    
     function fact(num)
     {
        var x = parseInt(num);
    
        if (x == 0 || x == 1) return 1;
    
        mem[x] = x * fact(x-1);
    
        return mem[x];
     }
    

提交回复
热议问题