1: /// -------- ToujoursEnBeta
2: /// Author & Copyright : Peter Luschny
3: /// License: LGPL version 3.0 or (at your option)
4: /// Creative Commons Attribution-ShareAlike 3.0
5: /// Comments mail to: peter(at)luschny.de
6: /// Created: 2010-03-01
7:
8: namespace Sharith.Math.Factorial
9: {
10: using System.Threading.Tasks;
11: using XInt = Sharith.Arithmetic.XInt;
12: using XMath = Sharith.Math.MathUtils.XMath;
13:
14: public class SquaredDiffProd : IFactorialFunction
15: {
16: public SquaredDiffProd() { }
17:
18: public string Name
19: {
20: get { return "SquaredDiffProduct "; }
21: }
22:
23: public XInt Factorial(int n)
24: {
25: if (n < 0)
26: {
27: throw new System.ArgumentOutOfRangeException("n",
28: Name + ": n >= 0 required, but was " + n);
29: }
30:
31: if (n < 7)
32: {
33: return (XInt)(new int[] { 1, 1, 2, 6, 24, 120, 720 })[n];
34: }
35:
36: long h = n / 2;
37: long q = h * h;
38: var f = new long[(int)h];
39: f[0] = (n & 1) == 1 ? 2 * q * n : 2 * q;
40: int i = 1;
41:
42: for (int d = 1; d < n - 2; d += 2)
43: {
44: f[i++] = q -= d;
45: }
46:
47: return XMath.Product(f, f.Length);
48: }
49: }
50: } //endOfFactorialSquaredDiffProd