There are two things you should learn if you need to calculate a factorial function:
1) Memoization . This will greatly speed up your calculations, since a factor function has a recursive definition. What you do is the previous calculations in the cache, so when you request k! , you can get it in one step by calculating k*((k-1)!) if you have (k-1)! cached.
2) Stirling shift . If you need to calculate large factorials, you can quickly approximate them in this way and with guaranteed error limits so that you can determine if the approximation is acceptable for your application.
If you do nothing, you will find that there is a relatively small k for which you simply cannot calculate k! in a reasonable amount of time.
uckelman
source share