BigInteger.isProbablePrime() method in Java

TheBigInteger.isProbablePrime(int certainty) returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is ≤ 0, true is returned.

Here, the “certainty” parameter is 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/2certainty). The execution time of this method is proportional to the value of this parameter.

The following is an example −


 Live Demo

import java.math.BigInteger;
public class Demo {
   public static void main(String[] argv) throws Exception {
      // create 3 BigInteger objects
      BigInteger bi1, bi2;
      // create 3 Boolean objects
      Boolean b1, b2, b3;
      bi1 = new BigInteger("11");
      bi2 = new BigInteger("20");
      // isProbablePrime()
      b1 = bi1.isProbablePrime(1);
      b2 = bi2.isProbablePrime(1);
      String str1 = bi1+ " is prime with certainty 1 is " +b1;
      String str2 = bi2+ " is prime with certainty 1 is " +b2;


11 is prime with certainty 1 is true
20 is prime with certainty 1 is false

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started