Ejemplo n.º 1
0
        static List <PExp> Factor(BigInteger n)
        {
            List <PExp> pf = new List <PExp>();
            BigInteger  nn = n;
            int         e  = 0;

            while (!nn.BitTest(e))
            {
                e++;
            }
            if (e > 0)
            {
                nn = nn >> e;
                pf.Add(new PExp(2, e));
            }
            BigInteger s = nn.Sqrt();
            BigInteger d = 3;

            while (nn > 1)
            {
                if (d > s)
                {
                    d = nn;
                }
                e = 0;
                while (true)
                {
                    BigInteger div = BigInteger.DivRem(nn, d, out BigInteger rem);
                    if (rem.BitLength() > 0)
                    {
                        break;
                    }
                    nn = div;
                    e++;
                }
                if (e > 0)
                {
                    pf.Add(new PExp(d, e));
                    s = nn.Sqrt();
                }
                d = d + 2;
            }

            return(pf);
        }