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)
你需要一个BigInteger.它可以容纳任意大数.
但在你的情况下100000000!是如此庞大的数字,没有什么可以帮助.
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.