Most efficient way to write Combination and Permutation calculator in Javascript

前端 未结 5 971
闹比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:46

    If you're concerned about efficiency, you'd probably want to re-implement the factorial as an iterative function rather than a recursive one. The recursive version will use a lot more memory and CPU time than the iterative version.

    function factorial(n) { 
      var x=1; 
      var f=1;
      while (x<=n) {
        f*=x; x++;
      }
        return f;
    }
    

    You also shouldn't be adding your own functions to the Math namespace. It's not a good habit to get into.

提交回复
热议问题