Advertisement
DulcetAirman

Factorial

Jul 3rd, 2013
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     private static BigInteger TWO = BigInteger.valueOf(2);
  2.     public static BigInteger facBigIntFast(final int n) {
  3.         if (n < 4)
  4.             return BigInteger.valueOf(n < 2 ? 1 : n == 2 ? 2 : 6);
  5.         final long h = n / 2;
  6.         BigInteger q = BigInteger.valueOf(h * h);
  7.         BigInteger f = (n & 1) == 1 ? TWO.multiply(q).multiply(BigInteger.valueOf(n)) : TWO.multiply(q);
  8.         for (int d = 1; d < n - 2; d += 2) {
  9.             q = q.subtract(BigInteger.valueOf(d));
  10.             f = f.multiply(q);
  11.         }
  12.         return f;
  13.     }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement