Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. static BigInteger nextProbablePrime(BigInteger n) {
  2. // PRE: n >= 0
  3. int i, j;
  4. // int certainty;
  5. int gapSize = 1024; // for searching of the next probable prime number
  6. int[] modules = new int[primes.length];
  7. boolean isDivisible[] = new boolean[gapSize];
  8. BigInt ni = n.getBigInt();
  9. // If n < "last prime of table" searches next prime in the table
  10. if (ni.bitLength() <= 10) {
  11. int l = (int)ni.longInt();
  12. if (l < primes[primes.length - 1]) {
  13. for (i = 0; l >= primes[i]; i++) {}
  14. return BIprimes[i];
  15. }
  16. }
  17.  
  18. BigInt startPoint = ni.copy();
  19. BigInt probPrime = new BigInt();
  20.  
  21. // Fix startPoint to "next odd number":
  22. startPoint.addPositiveInt(BigInt.remainderByPositiveInt(ni, 2) + 1);
  23.  
  24. // // To set the improved certainty of Miller-Rabin
  25. // j = startPoint.bitLength();
  26. // for (certainty = 2; j < BITS[certainty]; certainty++) {
  27. // ;
  28. // }
  29.  
  30. // To calculate modules: N mod p1, N mod p2, ... for first primes.
  31. for (i = 0; i < primes.length; i++) {
  32. modules[i] = BigInt.remainderByPositiveInt(startPoint, primes[i]) - gapSize;
  33. }
  34. while (true) {
  35. // At this point, all numbers in the gap are initialized as
  36. // probably primes
  37. Arrays.fill(isDivisible, false);
  38. // To discard multiples of first primes
  39. for (i = 0; i < primes.length; i++) {
  40. modules[i] = (modules[i] + gapSize) % primes[i];
  41. j = (modules[i] == 0) ? 0 : (primes[i] - modules[i]);
  42. for (; j < gapSize; j += primes[i]) {
  43. isDivisible[j] = true;
  44. }
  45. }
  46. // To execute Miller-Rabin for non-divisible numbers by all first
  47. // primes
  48. for (j = 0; j < gapSize; j++) {
  49. if (!isDivisible[j]) {
  50. probPrime.putCopy(startPoint);
  51. probPrime.addPositiveInt(j);
  52. if (probPrime.isPrime(100)) {
  53. return new BigInteger(probPrime);
  54. }
  55. }
  56. }
  57. startPoint.addPositiveInt(gapSize);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement