可以使用哪种数据类型来保持100,000,000的阶乘值?

Mal*_*qib 0 java types numbers

数据类型保持一个非常大的数字说1000或更多数字?我需要找到一个大数据的阶乘表示100000000.我的阶乘程序适用于较小的数字.

long factorial(int x)
{
    long fact=1;
    if(x<0)
            {
        System.out.println("Incorrect input, enter a positive number");
        fact=0;
    }
    if(x==0)
        fact=1;
    if(x>0)
            {
            fact=x;
        fact=fact*factorial(x-1);

    }
    return fact;
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*hev 7

你需要一个BigInteger.它可以容纳任意大数.

但在你的情况下100000000!是如此庞大的数字,没有什么可以帮助.

  • 这是他最好的选择,这是真的.但如果他对*一亿*的阶乘感兴趣,那么所有宇宙中的所有基本粒子都无法帮助他. (2认同)

duf*_*ymo 5

You should use a log of gamma function, since gamma(n) = (n-1)! Far more efficient than your naive, student way of doing things. It'll be a double in that case, and the natural log will grow more slowly than the value does.

Recursion? Please. Your problem won't be the size of the value you pass back - you'll get too many stack frames and out of memory error long before that.