Most efficient way to write Combination and Permutation calculator in Javascript

前端 未结 5 876
闹比i
闹比i 2021-01-12 10:05

I have a math website http://finitehelp.com that teaches students Finite Math. I thought it would be cool to include a calculator so I made one for combinations and permutat

5条回答
  •  离开以前
    2021-01-12 10:45

    I would prefer recursive function, tail recursive may cause stackoverflow for functions like fibonacci.

    Math._factorial = function(n){
      return Math._fact(n,1);
    }
    
    Math._fact= function(n,res){
      n = Number(n);
      if (n == null) {
        alert("Factorial requires a numeric argument.");
        return null;
      } else if (n < 2){
        return res;
      } else {
        return Math._fact(n-1, res*n);
      }
    }
    

提交回复
热议问题