Пример #1
0
        /**
         * It uses the sieve of Eratosthenes to discard several composite numbers in
         * some appropriate range (at the moment {@code [this, this + 1024]}). After
         * this process it applies the Miller-Rabin test to the numbers that were
         * not discarded in the sieve.
         *
         * @see BigInteger#nextProbablePrime()
         * @see #millerRabin(BigInteger, int)
         */
        internal static BigInteger nextProbablePrime(BigInteger n)
        {
            // PRE: n >= 0
            int i, j;
            int certainty;
            int gapSize = 1024; // for searching of the next probable prime number

            int []     modules     = new int[primes.Length];
            bool[]     isDivisible = new bool[gapSize];
            BigInteger startPoint;
            BigInteger probPrime;

            // If n < "last prime of table" searches next prime in the table
            if ((n.numberLength == 1) && (n.digits[0] >= 0) &&
                (n.digits[0] < primes[primes.Length - 1]))
            {
                for (i = 0; n.digits[0] >= primes[i]; i++)
                {
                    ;
                }
                return(BIprimes[i]);
            }

            /*
             * Creates a "N" enough big to hold the next probable prime Note that: N <
             * "next prime" < 2*N
             */
            startPoint = new BigInteger(1, n.numberLength,
                                        new int[n.numberLength + 1]);
            java.lang.SystemJ.arraycopy(n.digits, 0, startPoint.digits, 0, n.numberLength);
            // To fix N to the "next odd number"
            if (n.testBit(0))
            {
                Elementary.inplaceAdd(startPoint, 2);
            }
            else
            {
                startPoint.digits[0] |= 1;
            }
            // To set the improved certainly of Miller-Rabin
            j = startPoint.bitLength();
            for (certainty = 2; j < BITS[certainty]; certainty++)
            {
                ;
            }
            // To calculate modules: N mod p1, N mod p2, ... for first primes.
            for (i = 0; i < primes.Length; i++)
            {
                modules[i] = Division.remainder(startPoint, primes[i]) - gapSize;
            }
            while (true)
            {
                // At this point, all numbers in the gap are initialized as
                // probably primes
                java.util.Arrays <Object> .fill(isDivisible, false);

                // To discard multiples of first primes
                for (i = 0; i < primes.Length; i++)
                {
                    modules[i] = (modules[i] + gapSize) % primes[i];
                    j          = (modules[i] == 0) ? 0 : (primes[i] - modules[i]);
                    for (; j < gapSize; j += primes[i])
                    {
                        isDivisible[j] = true;
                    }
                }
                // To execute Miller-Rabin for non-divisible numbers by all first
                // primes
                for (j = 0; j < gapSize; j++)
                {
                    if (!isDivisible[j])
                    {
                        probPrime = startPoint.copy();
                        Elementary.inplaceAdd(probPrime, j);

                        if (millerRabin(probPrime, certainty))
                        {
                            return(probPrime);
                        }
                    }
                }
                Elementary.inplaceAdd(startPoint, gapSize);
            }
        }