factorial

Fast way to calculate n! mod m where m is prime?

孤街醉人 提交于 2019-11-26 03:54:37
问题 I was curious if there was a good way to do this. My current code is something like: def factorialMod(n, modulus): ans=1 for i in range(1,n+1): ans = ans * i % modulus return ans % modulus But it seems quite slow! I also can\'t calculate n! and then apply the prime modulus because sometimes n is so large that n! is just not feasible to calculate explicitly. I also came across http://en.wikipedia.org/wiki/Stirling%27s_approximation and wonder if this can be used at all here in some way? Or,

What is the fastest factorial function in JavaScript? [closed]

别来无恙 提交于 2019-11-26 02:41:57
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 months ago . Looking for a really fast implementation of factorial function in JavaScript. Any suggests? 回答1: You can search for (1...100)! on WolframAlpha to pre-calculate the factorial sequence. The first 100 numbers are: 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,