我应该优化下面的代码,以便它计算中心二项系数达到整数的最大值(最多n = 16)。Java:避免阶乘溢出
public static int factorial(int n)
{
int result= 1;
for(int i = 2; i <= n; i++) result *= i;
return result;
}
public static int centralbinom(int n)
{
return factorial(2*n)/(factorial(n) * factorial(n));
}
当然,我得到一个溢出每n> 6. 如何“打破”阶乘功能,使不具有处理大的数字,如为2n = 2 * 16 = 32?
还是有更好的方法来计算中央二项式系数?
你得到7的溢出! ? – NWard
@NWard 14! (2 * N) –
你有没有想过使用BigInteger? –