16

I just want to confirm my intuition about this method. Consider the code below.

long knownPrime = // some large known prime
int certainty = // some integer greater than 0

BigInteger b = BigInteger.valueOf(knownPrime);
boolean isPrime = b.isProbablePrime(certainty);

For a large known prime, and for any certainty > 0, is it accurate to say that b.isProbablePrime(certainty) will always return true?

Or are there any cases where the method "guesses" that a known prime number is composite?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
ktm5124
  • 11,861
  • 21
  • 74
  • 119
  • Perhaps I completely misinterpreted the documentation the way I read it, if the value of certainty increases, then the accuracy of the answer should also increase? If that is correct, then yes, it should always return true, with a certainty of 1 would be the only point of potential error or "guessing." Of course I could have completely misunderstood that. – Brandon Buck Mar 19 '15 at 21:29
  • Actually, an answer to you question is contained in the method name. This method doesn't guess if number is composite, it guesses the opposite condition, i.e. method returns false (with any certainty) only if it's sure that number is composite. – bsiamionau Mar 19 '15 at 21:38

4 Answers4

11

For a large known prime, and for any certainty > 0, is it accurate to say that b.isProbablePrime(certainty) will always return true?

Yes. The documentation says it will return false only if it is certain that the number is composite.

Returns: true if this BigInteger is probably prime, false if it's definitely composite.

So the certainty parameter will influence only on the chance of a false-positive: saying a composite number is prime, when it really is not.

Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
3

For a large known prime b, and for any certainty, b.isProbablePrime(certainty) returns true.

isProbablePrime can only err by returning true when the input is not prime (an example is b=6, certainty=0, which returns true), never the other way (because the Rabin-Miller test, which isProbablePrime uses, can only fail in this direction).

fgrieu
  • 2,724
  • 1
  • 23
  • 53
1

According to the documentation, this method will return

true if this BigInteger is probably prime, false if it's definitely composite.

So any definite (independently verified) prime number will, by definition, return true for any legal (>0) certainty/tolerance value is passed in. The only thing the certainty/tolerance value changes is the occurrence of false-positives (true for composites) and the execution time of the method.

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
-1
import java.util.*;
import java.math.*;
public class Main
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        long l = s.nextLong();
        BigInteger b = new BigInteger(String.valueOf(l));
        BigInteger p = b.nextProbablePrime();
        System.out.println(p);
    }
}
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53