Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigInteger.isProbablePrime

Tags:

java

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?

like image 277
ktm5124 Avatar asked Mar 19 '15 21:03

ktm5124


People also ask

How do I use BigInteger isProbablePrime works?

BigInteger isProbablePrime() Method in Java with ExamplesThis method checks for prime or composite upon the current BigInteger by which this method is called and returns a boolean value. It returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is <= 0, true is returned.

What is certainty in BigInteger?

certainty Int32. a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2<sup> certainty </sup>). The execution time of this method is proportional to the value of this parameter.

How do you get prime in BigInteger?

The isProbablePrime() method of Java BigInteger class is used to determine if the given number is prime or not. For certainty =1, this method returns true if this BigInteger is prime and false if this BigInteger is composite.

How big can BigInteger get?

The BigInteger class allows you to create and manipulate integer numbers of any size. The BigInteger class stores a number as an array of unsigned, 32-bit integer "digits" with a radix, or base, of 4294967296.


2 Answers

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.

like image 111
Anderson Vieira Avatar answered Nov 04 '22 09:11

Anderson Vieira


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).

like image 35
fgrieu Avatar answered Nov 04 '22 08:11

fgrieu