Advertisement
DulcetAirman

Factorial

Jul 3rd, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  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
Add Comment
Please, Sign In to add comment
Advertisement