Java BigInteger Class | probablePrime() Method with Example

BigInteger Class probablePrime() method: Here, we are going to learn about the probablePrime() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 11, 2020

BigInteger Class probablePrime() method

  • probablePrime() method is available in java.math package.
  • probablePrime() method is used to get the BigInteger that holds the positive probable prime value with the given length.
  • probablePrime() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • probablePrime() method may throw an exception at the time of getting probable prime.
    ArithmeticException: This exception may throw when the given first parameter value is not in a range.

Syntax:

    public BigInteger probablePrime(int len, Random ran);

Parameter(s):

  • int len – represents the length of bits to denote this BigInteger value.
  • Random ran – represents the list of random bits to choose for checking prime value.

Return value:

The return type of this method is BigInteger, it returns BigInteger that holds the prime value with the defined length of bits.

Example:

// Java program to demonstrate the example 
// of BigInteger probablePrime(int len, Random ran)
// method of BigInteger

import java.math.*;
import java.util.*;

public class ProbablePrimeOfBI {
    public static void main(String args[]) {
        // Initializes a variables bit_len
        int bit_len = 4;

        // Initializes a Random object  
        Random ran = new Random();

        // Here, this method probablePrime(int,Random)
        // is used to return the random prime
        // number with the given bit length i.e. it
        // returns randomly 11 so the bit length of 
        // 11 is 4 like 1011
        BigInteger prime = BigInteger.probablePrime(bit_len, ran);
        System.out.println("BigInteger.probablePrime(bit_len,ran): " + prime);
    }
}

Output

BigInteger.probablePrime(bit_len,ran): 11


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.