Java BigInteger probablePrime() method

The probablePrime() method of Java BigInteger class is used to find a probably prime number of any bit length. This method returns a positive BigInteger that is probably prime, with the specified bitLength.

Syntax:

Parameter:

bitLength- bitLength of the returned BigInteger.

rnd- source of random bits used to select candidates to be tested for primality.

Returns:

This method returns a BigInteger of bitLength bits that is probably prime.

Exception:

ArithmeticException- if bitLength < 2.

Note: The probability that a BigInteger returned by this method is composite does not exceed 2-100.

Example 1

Test it Now

Output:

ProbablePrime of bitlength 4 is 11

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.ArithmeticException: bitLength < 2
        at java.math.BigInteger.probablePrime(BigInteger.java:695)
        at BigIntegerProbablePrimeExample2.main(BigIntegerProbablePrimeExample2.java:13)

Example 3

Test it Now

Output:

ProbablePrime of bitlength 10 is 857

Next TopicJava BigInteger




Latest Courses