BigInteger probablePrime() in Java

probablePrime(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.probablePrime(int bitLength, Random rnd)

This method takes two arguments, one of type int and another of type Random as its parameters. This method returns a positive BigInteger that is probably prime, with the specified bitLength. The probability that a BigInteger returned by this method is composite does not exceed 2^-100 (or 2 power -100).

Parameters: Two parameters are required for this method.

bitLength: bitLength of the returned BigInteger.rnd source of random bits used to select candidates to be tested for primality.

Returns: a BigInteger of bitLength bits that is probably prime.

Throws: ArithmeticException - bitLength < 2 or bitLength is too large.

For Example:

BigInteger bigInteger = new BigInteger("12349919")

int bitLength = 100

Random rnd = new Random(7)

bigInteger.probablePrime(bitLength,rnd) = > It returns 720190635270599018774509764457.

Approach

Java

import java.math.BigInteger;
import java.util.Random;

public class BigIntegerprobablePrime {
    @SuppressWarnings("static-access")
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("12349919");

        int bitLength = 100;
        Random rnd = new Random(7);

        System.out.println(bigInteger.probablePrime(bitLength,
 rnd));
    }
}

Output:

720190635270599018774509764457

No comments:

Post a Comment