1:  package de.luschny.math.factorial;
   2:   
   3:  import de.luschny.math.arithmetic.Xint;
   4:   
   5:  public class FactorialSquaredDiff implements IFactorialFunction
   6:  {
   7:   
   8:      public FactorialSquaredDiff()
   9:      {
  10:      }
  11:   
  12:      public String getName()
  13:      {
  14:          return "SquaredDifference ";
  15:      }
  16:   
  17:      public Xint factorial(int n)
  18:      {
  19:          if (n < 0)
  20:          {
  21:              throw new ArithmeticException("Factorial: n has to be >= 0, but was " + n);
  22:          }
  23:   
  24:          if (n < 2)
  25:          {
  26:              return Xint.ONE;
  27:          }
  28:   
  29:          long h = n / 2, q = h * h;
  30:          long r = (n & 1) == 1 ? 2 * q * n : 2 * q;
  31:          Xint f = Xint.valueOf(r);
  32:   
  33:          for (int d = 1; d < n - 2; d += 2)
  34:          {
  35:              f = f.multiply(q -= d);
  36:          }
  37:   
  38:          return f;
  39:      }
  40:   
  41:  } // endOfFactorialSquaredDiff