コード例 #1
0
        public string Factorial(int n)
        {
            if (n < 0)
            {
                throw new System.ArgumentException(message: nameof(n) + " >= 0 required, but was " + n);
            }

            if (n < 2)
            {
                return("1");
            }

            var p = new DecInteger(1);
            var r = new DecInteger(1);

            this.n = 1;

            int h = 0, shift = 0, high = 1;
            var log2N = (int)System.Math.Floor(System.Math.Log(n) * 1.4426950408889634);

            while (h != n)
            {
                shift += h;
                h      = n >> log2N--;
                var len = high;
                high = (h - 1) | 1;
                len  = (high - len) / 2;

                if (len > 0)
                {
                    p = p * this.Product(len);
                    r = r * p;
                }
            }

            r = r * DecInteger.Pow2(shift);
            return(r.ToString());
        }
コード例 #2
0
        public string Factorial(int n)
        {
            if (n < 0)
            {
                throw new System.ArgumentException(message: nameof(n) + " >= 0 required, but was " + n);
            }

            if (n < 2)
            {
                return "1";
            }

            var p = new DecInteger(1);
            var r = new DecInteger(1);

            this.n = 1;

            int h = 0, shift = 0, high = 1;
            var log2N = (int)System.Math.Floor(System.Math.Log(n) * 1.4426950408889634);

            while (h != n)
            {
                shift += h;
                h = n >> log2N--;
                var len = high;
                high = (h - 1) | 1;
                len = (high - len) / 2;

                if (len > 0)
                {
                    p = p * this.Product(len);
                    r = r * p;
                }
            }

            r = r * DecInteger.Pow2(shift);
            return r.ToString();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: arceus6666/p2simulacion
        private static void poisson(double[] r, double lambda)
        {
            int l = r.Length;

            int Factorial(int n)
            {
                if (n < 0)
                {
                    return(0);
                }
                if (n < 2)
                {
                    return(1);
                }
                DecInteger pp = new DecInteger(1);
                DecInteger rr = new DecInteger(1);

                N = 1;
                int h = 0, shift = 0, high = 1;
                int log2n = (int)Math.Floor(Math.Log(n) * 1.4426950408889634);

                while (h != n)
                {
                    shift += h;
                    h      = n >> log2n--;
                    int len = high;
                    high = (h - 1) | 1;
                    len  = (high - len) / 2;
                    if (len > 0)
                    {
                        pp *= Product(len);
                        rr *= pp;
                    }
                }
                rr *= DecInteger.Pow2(shift);
                return(int.Parse(rr.ToString()));
            }

            double p(int x) => Math.Pow(lambda, x) * Math.Pow(Math.E, -lambda) / Factorial(x);

            //double[] px = new double[10];
            double[] pxa = new double[11];
            pxa[0] = 0;
            //double prev = 0;
            string[] zeros = { "", "0", "00", "000" };
            for (int i = 0; i < 10; i++)
            {
                pxa[i + 1] = p(i) + pxa[i];
            }
            //Console.WriteLine("pxa1:\n" + printArray(pxa));
            for (int i = 0; i < pxa.Length; i++)
            {
                double pi  = pxa[i];
                int    pv  = (int)(pi * 10000);
                int    pvs = 4 - (pv + "").Length;
                double dpv;
                if (pvs < 0)
                {
                    dpv = 1;
                }
                else
                {
                    dpv = double.Parse($"0.{zeros[pvs]}{pv}");
                }
                pxa[i] = dpv;
            }
            //Console.WriteLine("pxa2:\n" + printArray(pxa));
            double[] xi = new double[l];
            for (int i = 0; i < l; i++)
            {
                for (int j = 0; j < pxa.Length; j++)
                {
                    if (pxa[j] < r[i] && r[i] < pxa[j + 1])
                    {
                        xi[i] = j;
                        break;
                    }
                }
            }
            //Console.WriteLine("xi:\n"+printArray(xi));
            string res = "+-----+--------+----+\n";

            res += "|  i  |   ri   | xi |\n";
            res += "+-----+--------+----+\n";
            for (int i = 0; i < l; i++)
            {
                int il  = (i + "").Length;
                int ril = (r[i] + "").Length;
                int xil = (xi[i] + "").Length;
                Console.WriteLine($"{i} {ril} {xil}");
                res += $"| {i}{blanks[4 - il]}| {r[i]}{blanks[7 - ril]}| {xi[i]}{blanks[3 - xil]}|\n";
            }
            res += "+------+--------+----+\n";
            Console.WriteLine(res);
        }