Java Data Type How to - Check if a BigInteger a Probable Prime








Question

We would like to know how to check if a BigInteger a Probable Prime.

Answer

     /*w  ww .  j a  v  a 2 s. c  om*/
import java.math.BigInteger;

public class BigNumApp {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}

The code above generates the following result.