简体   繁体   中英

Is BigInteger#nextProbablePrime accurate up to, at least, 64-bit numbers?

I'm looking to generate prime numbers using BigInteger#nextProbablePrime . The documentation of this method states the following:

The probability that the number returned by this method is composite does not exceed 2^(-100).

Is it safe to assume that every value that this method returns (between BigInteger.TWO and BigInteger.valueOf(Long.MAX_VALUE) ) is prime?

Statistically, no. There is a known probability that it could be wrong.

Realistically, yes. The chance of an incorrect value is extremely low.

The probability that the number returned by this method is composite does not exceed 2^-100

There are 2^63-ish numbers between 2 and Long.MAX_VALUE . If we assume each number in a long is independent we get that the probability of one number being wrong is (1-2^-100), the probability of none of these being wrong is (1-2^100)^(2^63), which is pretty much 0.

In other words no.

That being said you would have to be very unlucky to actually find one of these numbers.

Edit with some slightly fancier maths:

If we assume that instead of each number between 2 and 2^63 is independent, and instead assume each prime is independent: There are 2^63/Log[2^63] (1 + 1.2762/Log[2^63]) = 2.17387*10^17 primes or less between 2 and 2^63 . That leaves you with a probability of all of them being correct as:

N[Exp[Log[1 - 2^-100]*(2^63/Log[2^63] (1 + Rationalize[1.2762]/Log[2^63]))], 1000] 
== 0.99999999999982851172...

(I took the exp of the log to avoid numerical troubles).

This answer is the correct one. I'm going to expand on it just a little more.

The actually probabilities involved are more complicated that the documentation states, and are much smaller. But the probability quoted in the docs is usually the one you want, because it is a worst case probability. And providing a more detailed answer would be too mathematical. Your welcome to see the analysis yourself .

Another thing worth noting: A completely deterministic, 100% correct, primality test is available for positive longs that would be much faster than the current test. The designers of the BigInteger class evidently concluded that coding for this special case would not be worth the maintenance cost. I disagree and I intend to file a bug/feature report suggesting this.

"safe" is relative here. You need a deterministic test on each result of.probablePrime() to be 100% sure. This method do not work with 100% accuracy, even to 64bit numbers.

Any time that there is a probability that something will or will not occur, then it definitely, will or will not occur. The real question is, can you deal with the extremely remote possibility of an event happening?

The other answers correctly state that it's not guaranteed but the probability of a false result is very low. I guess what you might be interested in is: is it guaranteed on your current implementation of BigInteger ?

Let's imagine you're using OpenJDK 13.0.1 on Hotspot on Linux and you're going to stay with that for a long while. Let's say you've written a test that confirmed that every single "probable prime" returned by this particular version of BigInteger, up to 2^31-1, is indeed a prime. It took your test many hours to run, but that's ok, you only need to run it once in your lifetime, no?

Given the above, can you now be sure that this particular version of BigInteger is guaranteed to return actual primes the next time you run it? Probably, but there's no guarantee stated in the docs that BigInteger doesn't use some randomization that gives different results in different sessions. However, you can examine its source code to be sure of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM