Difficulty with BigInteger

前端 未结 4 1973
北海茫月
北海茫月 2021-01-15 05:18

I am trying to do Factorial with Recursion and BigIntegers but eclipse is complaining about the BigInteger. I know the program is supposed to be simple but it is giving me h

4条回答
  •  误落风尘
    2021-01-15 05:48

    In addition to what @aix mentioned regarding invoking the arithmetics on BigInteger - I can also see another issue with this code.

    Your method signature is

    public static  int fact(BigInteger n)
    

    This is problemantic - factorial grows fast, so you are very likely to overflow the result.
    I think what you realy wanted is:

    public static  BigInteger fact(int n)
    

    which makes much more sense, since the return value should probably be the BigInteger (since it grows fast) and not the parameter, or possibly - both of them.

提交回复
热议问题