예제 #1
0
        protected override FactorizationResult Process(BigInteger n, BigInteger p, BigInteger q, int coefficient,
                                                       CancellationToken cancellationToken)
        {
            p = p * coefficient;
            q = n - p;

            BigInteger d;

            do
            {
                d = BigIntegerExtentions.GreatestCommonDivisor(p, q);

                if (d > 1)
                {
                    continue;
                }

                p--;
                q++;
            } while (d == 1 && !cancellationToken.IsCancellationRequested);

            return(cancellationToken.IsCancellationRequested
                ? FactorizationResult.Failed
                : new FactorizationResult(d, n / d));
        }
        protected virtual FactorizationResult Process(BigInteger n, BigInteger p, BigInteger q,
                                                      int coefficient, CancellationToken cancellationToken)
        {
            p = p / coefficient;
            q = q * coefficient;

            BigInteger delta = n - p * q;

            while (!cancellationToken.IsCancellationRequested)
            {
                if (delta == 0)
                {
                    return(new FactorizationResult(p, q));
                }

                if (delta > 0)
                {
                    q++;
                    delta -= p; // for optimization
                }

                if (delta < 0)
                {
                    BigInteger x1, x2;
                    if (BigIntegerExtentions.TrySolveQuadraticEquation(coefficient, q - coefficient * p, delta,
                                                                       out x1, out x2) &&
                        (x1 > 0 || x2 > 0))
                    {
                        BigInteger i = x1 > 0 ? x1 : x2;
                        q += i;
                        p -= i * coefficient;

                        delta = n - p * q;
                    }
                    else
                    {
                        p--;
                        delta += q; // for optimization
                    }
                }
            }

            return(FactorizationResult.Failed);
        }
예제 #3
0
        protected override FactorizationResult Process(BigInteger n, int threadNumber, int threadCount,
                                                       CancellationToken cancellationToken)
        {
            for (BigInteger z = n.Sqtr() + threadCount; !cancellationToken.IsCancellationRequested; z += threadNumber)
            {
                BigInteger p = BigInteger.ModPow(z, 2, n);

                if (!p.IsFullSquare())
                {
                    continue;
                }

                BigInteger q = BigIntegerExtentions.GreatestCommonDivisor(z - p.Sqtr(), n);
                if (q > 1)
                {
                    return(new FactorizationResult(q, n / q));
                }
            }

            return(FactorizationResult.Failed);
        }