Coders Packet

How to run isProbablePrime method in Java

By Kashish Naresh Dhoot

In this tutorial, we have to check whether the given number is prime or not using the Java BigInteger class isProbablePrime method.

A Prime number is a natural number greater than 1 and whose only positive divisors are 1 and the number itself. Example: 2,3,5,7,11,13...etc.

Here, isProbablePrime returns true if BigInteger is probable prime. false if it's definitely composite. If certainty is <=0, true is returned.

 

import java.util.*;
public class isprobableprime 
{
  private static final Scanner scanner = new Scanner(System.in);
  public static void main(String[] args) 
  {
        java.math.BigInteger n = scanner.nextBigInteger();
        Boolean a=n.isProbablePrime(1);
        if(a==true)
        {
            System.out.println("prime");

        }
        else
        {
            System.out.println("not prime");

        }
        

        scanner.close();
    }
}

 

Output:- 

               output1         

              Output2       

Download project

Reviews Report

Submitted by Kashish Naresh Dhoot (Kashish1104)

Download packets of source code on Coders Packet