1:  package de.luschny.math.factorial;
   2:   
   3:  import de.luschny.math.arithmetic.Xint;
   4:   
   5:  public class FactorialSquaredDiffProd implements IFactorialFunction
   6:  {
   7:   
   8:      public FactorialSquaredDiffProd()
   9:      {
  10:      }
  11:   
  12:      public String getName()
  13:      {
  14:          return "SquaredDiffProduct";
  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 < 4)
  25:          {
  26:              return Xint.valueOf(n < 2 ? 1 : n == 2 ? 2 : 6);
  27:          }
  28:   
  29:          long h = n / 2, q = h * h;
  30:          long[] f = new long[(int) h];
  31:          f[0] = (n & 1) == 1 ? 2 * q * n : 2 * q;
  32:          int i = 1;
  33:   
  34:          for (int d = 1; d < n - 2; d += 2)
  35:          {
  36:              f[i++] = q -= d;
  37:          }
  38:   
  39:          return Xint.product(f);
  40:      }
  41:   
  42:  } // endOfFactorialSquaredrDiffProd