Thursday, July 4, 2024
HomeLanguagesJavaBigInteger isProbablePrime() Method in Java with Examples

BigInteger isProbablePrime() Method in Java with Examples

The java.math.BigInteger.isProbablePrime(int certainty) method is used to tell if this BigInteger is probably prime or if it’s definitely composite. This method checks for prime or composite upon the current BigInteger by which this method is called and returns a boolean value. It returns true if this BigInteger is probably prime, false if it’s definitely composite. If certainty is <= 0, true is returned.

Syntax:

public boolean isProbablePrime(int certainty)

Parameters: This method accepts a mandatory parameter certainty which is a measure of the uncertainty that is acceptable to the user. This is due to the fact the BigInteger is a very very large number and finding exactly if it is prime is very difficult and expensive. Hence it can be said that this method checks for the prime of this BigInteger based on a threshold value (1 – 1/2certainty).

Return Value: This method returns a boolean value stating whether this BigInteger is prime or not. It returns true if this BigInteger is probably prime, false if it’s definitely composite.

Below program is used to illustrate the isProbablePrime() method of BigInteger.

Example 1:




// Java program to demonstrate
// isProbablePrime() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Boolean variable to store the result
        boolean result;
  
        // Creates one BigInteger object
        BigInteger a
            = new BigInteger(
                "95848961698036841689418631330196");
  
        // When certainty is one,
        // it will check number for prime or composite
        result = a.isProbablePrime(1);
        System.out.println(a.toString()
                           + " with certainty 1 "
                           + result);
  
        // When certainty is zero,
        // it is always true
        result = a.isProbablePrime(0);
        System.out.println(a.toString()
                           + " with certainty 0 "
                           + result);
  
        // When certainty is negative,
        // it is always true
        result = a.isProbablePrime(-1);
        System.out.println(a.toString()
                           + " with certainty -1 "
                           + result);
    }
}


Output:

95848961698036841689418631330196 with certainty 1 false
95848961698036841689418631330196 with certainty 0 true
95848961698036841689418631330196 with certainty -1 true

Example 2:




// Java program to demonstrate
// isProbablePrime() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Boolean variable to store the result
        boolean result;
  
        // Creates one BigInteger object
        BigInteger a
            = new BigInteger(
                "654561561356879113561");
  
        // When certainty is one,
        // it will check number for prime or composite
        result = a.isProbablePrime(1);
        System.out.println(a.toString()
                           + " with certainty 1 "
                           + result);
  
        // When certainty is zero,
        // it is always true
        result = a.isProbablePrime(0);
        System.out.println(a.toString()
                           + " with certainty 0 "
                           + result);
  
        // When certainty is negative,
        // it is always true
        result = a.isProbablePrime(-1);
        System.out.println(a.toString()
                           + " with certainty -1 "
                           + result);
    }
}


Output:

654561561356879113561 with certainty 1 false
654561561356879113561 with certainty 0 true
654561561356879113561 with certainty -1 true

Reference: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#isProbablePrime(int)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments