// Get primes through specified bound (inclusive) and Integer.MAX_VALUE
NavigableSet<BigInteger> primes = getPrimes(upperBound);
// Check whether known primes are identified as such boolean primeTest = checkPrime(primes, certainty, parallel);
System.out.println("Prime test result: " + (primeTest ? "SUCCESS" : "FAILURE")); if (!primeTest) {
System.err.println("Prime test failed");
}
// Check whether known non-primes are not identified as primes boolean nonPrimeTest = checkNonPrime(primes, certainty);
System.out.println("Non-prime test result: " + (nonPrimeTest ? "SUCCESS" : "FAILURE"));
// Create a list of non-prime BigIntegers.
SplittableRandom splitRandom = RandomFactory.getSplittableRandom();
List<BigInteger> nonPrimeBigInts = (splitRandom)
.ints(NUM_NON_PRIMES, 2, maxPrime).mapToObj(BigInteger::valueOf)
.filter(b -> !b.isProbablePrime(certainty)).collect(toList());
// If there are any non-probable primes also in the primes list then fail. boolean failed = nonPrimeBigInts.stream().anyMatch(primes::contains);
// In the event, print which purported non-primes were actually prime. if (failed) { for (BigInteger bigInt : nonPrimeBigInts) { if (primes.contains(bigInt)) {
System.err.println("Prime value thought to be non-prime: " + bigInt);
}
}
}
return !failed;
}
/** *VerifieswhetheraspecifiedsubsetofMersenneprimesarecorrectly *identifiedasbeingprime.See *<ahref="https://en.wikipedia.org/wiki/Mersenne_prime">Mersenne prime</a> *formoreinformation. * *@returntrueifandonlyifthetestsucceeds
*/ privatestaticboolean checkMersennePrimes(int certainty) { int[] MERSENNE_EXPONENTS = { 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, // uncomment remaining array elements to make this test run a long time /* 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243,110503,132049,216091,756839,859433,1257787,1398269, 2976221,3021377,6972593,13466917,20996011,24036583,25964951,
30402457, 32582657, 37156667, 42643801, 43112609, 57885161 */
};
System.out.println("Checking first "+MERSENNE_EXPONENTS.length+" Mersenne primes");
boolean result = true; for (int n : MERSENNE_EXPONENTS) {
BigInteger mp = BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); if (!mp.isProbablePrime(certainty)) {
System.err.println("Mp with p = "+n+" not classified as prime");
result = false;
}
}
return result;
}
privatestaticboolean checkHugeFails() { try { // huge odd integer
BigInteger a = BigInteger.ONE.shiftLeft(500_000_000 + 1)
.setBit(0);
a.isProbablePrime(1); // not expected to reach here returnfalse;
} catch (ArithmeticException e) { // this is the expected behavior returntrue;
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-10-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.