Java BigInteger Class | isProbablePrime() Method with Example

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

BigInteger Class isProbablePrime() method

  • isProbablePrime() method is available in java.math package.
  • isProbablePrime() method is used to check whether this BigInteger is probable prime or not.
  • isProbablePrime() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • isProbablePrime() method does not throw an exception at the time of checking probable prime.

Syntax:

    public boolean isProbablePrime(int probability);

Parameter(s):

  • int probability – represents the probability of being prime when probability <= 0.

Return value:

The return type of this method is boolean, it returns true when this BigInteger is probably prime otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean isProbablePrime(int probability) method  of BigInteger

import java.math.*;
public class IsProbablePrimeOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "13";
        String str2 = "6";

        // Initialize two BigInteger objects  
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);


        // checks whether this BigInteger
        // b_int1 value is prime or not , here the
        // value of b_int1 is 13 so it returns true
        // because 13 is prime
        boolean status = b_int1.isProbablePrime(2);
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int1.isProbablePrime(2): " + status);

        System.out.println();

        // checks whether this BigInteger
        // b_int2 value is prime or not , here the
        // value of b_int2 is 6 so it returns false
        // because 6 is not prime (i.e. composite)
        status = b_int2.isProbablePrime(2);
        System.out.println("b_int2: " + b_int2);
        System.out.println("b_int2.isProbablePrime(1): " + status);

        System.out.println();

        // checks whether this BigInteger
        // b_int2 value is prime or not , here the
        // value of b_int2 is 6 so it returns true
        // because 6 is not prime (i.e. composite) but
        // the given parameter value is -2 i.e. < 0 and
        // when the given parameter value <=0 so it 
        // returns true even the value of b_int2 is composite
        status = b_int2.isProbablePrime(-2);
        System.out.println("b_int2: " + b_int2);
        System.out.println("b_int2.isProbablePrime(-2): " + status);
    }
}

Output

b_int1: 13
b_int1.isProbablePrime(2): true

b_int2: 6
b_int2.isProbablePrime(1): false

b_int2: 6
b_int2.isProbablePrime(-2): true


Comments and Discussions!

Load comments ↻





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