Input Format

A single line containing an integer,  (the number to be checked).

Constraints

  •  contains at most  digits.

Output Format

If  is a prime number, print prime; otherwise, print not prime.


hackerrank java primality text solution


Java Primality test problem solution | HackerRank

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

     public static void main(String[] args) {
     Scanner in = new Scanner(System.in);
     BigInteger n = in.nextBigInteger();
     in.close();
      if(n.isProbablePrime(1)){
         System.out.println("prime");
      }
      else{
         System.out.println("not prime");
      }

    }
}

Second solution

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger n1,n2;

      // create 3 Boolean objects
      Boolean b1, b2, b3;

      // assign values to n1, n2
      n1 = new BigInteger("7");
      n2 = new BigInteger("9");

      // perform isProbablePrime on n1, n2
      b1 = n1.isProbablePrime(1);
      b2 = n2.isProbablePrime(1);
      b3 = n2.isProbablePrime(-1);

      // check if n1 is prime or not if certainity =1
      if(b1)
      System.out.println(n1 + " is prime with certainity 1" );
      else
      System.out.println(n1 + " is not prime with certainity 1" );
      
      // check if n2 is prime or not if certainity =1
      if(b2)
      System.out.println(n2 + " is prime with certainity 1" );
      else
      System.out.println(n2 + " is not prime with certainity 1" );
      
      // check if n2 is prime or not if certainity =-1
      if(b3)
      System.out.println(n2 + " is prime with certainity -1");
      else
      System.out.println(n2 + " is not prime with certainity -1" );
   }
}