1: using Xint = System.Numerics.BigInteger;
2:
3:
4: namespace Luschny.Math.Factorial
5: {
6: public class FactorialSquaredDiff : IFactorialFunction
7: {
8: public FactorialSquaredDiff() { }
9:
10: public string Name
11: {
12: get { return "SquaredDifference "; }
13: }
14:
15: public Xint Factorial(int n)
16: {
17: if (n < 0)
18: {
19: throw new System.ArgumentOutOfRangeException("n",
20: Name + ": n >= 0 required, but was " + n);
21: }
22:
23: if (n < 2)
24: {
25: return Xint.One;
26: }
27:
28: long h = n / 2, q = h * h;
29: long r = (n & 1) == 1 ? 2 * q * n : 2 * q;
30: Xint f = new Xint(r);
31:
32: for (int d = 1; d < n - 2; d += 2)
33: {
34: f *= q -= d;
35: }
36:
37: return f;
38: }
39: }
40: } //endOfFactorialSqrDiff