Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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];
- boolean isDivisible[] = new boolean[gapSize];
- BigInt ni = n.getBigInt();
- // If n < "last prime of table" searches next prime in the table
- if (ni.bitLength() <= 10) {
- int l = (int)ni.longInt();
- if (l < primes[primes.length - 1]) {
- for (i = 0; l >= primes[i]; i++) {}
- return BIprimes[i];
- }
- }
- BigInt startPoint = ni.copy();
- BigInt probPrime = new BigInt();
- // Fix startPoint to "next odd number":
- startPoint.addPositiveInt(BigInt.remainderByPositiveInt(ni, 2) + 1);
- // // To set the improved certainty 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] = BigInt.remainderByPositiveInt(startPoint, primes[i]) - gapSize;
- }
- while (true) {
- // At this point, all numbers in the gap are initialized as
- // probably primes
- Arrays.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.putCopy(startPoint);
- probPrime.addPositiveInt(j);
- if (probPrime.isPrime(100)) {
- return new BigInteger(probPrime);
- }
- }
- }
- startPoint.addPositiveInt(gapSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement