StackOverflowError computing factorial of a BigInteger?

后端 未结 5 1933
有刺的猬
有刺的猬 2020-12-01 18:33

I am trying to write a Java program to calculate factorial of a large number. It seems BigInteger is not able to hold such a large number.

The below is

5条回答
  •  长情又很酷
    2020-12-01 19:09

    Naive implementations of factorial don't work out in real situations.

    If you have a serious need, the best thing to do is to write a gamma function (or ln(gamma) function) that will work not only for integers but is also correct for decimal numbers. Memoize results so you don't have to keep repeating calculations using a WeakHashMap and you're in business.

提交回复
热议问题