private static BigInteger TWO = BigInteger.valueOf(2);
public static BigInteger facBigIntFast(final int n) {
if (n < 4)
return BigInteger.valueOf(n < 2 ? 1 : n == 2 ? 2 : 6);
final long h = n / 2;
BigInteger q = BigInteger.valueOf(h * h);
BigInteger f = (n & 1) == 1 ? TWO.multiply(q).multiply(BigInteger.valueOf(n)) : TWO.multiply(q);
for (int d = 1; d < n - 2; d += 2) {
q = q.subtract(BigInteger.valueOf(d));
f = f.multiply(q);
}
return f;
}
Comments