Type Here to Get Search Results !

Java Primality Test | HackerRank Solution By CodingHumans |

0

Java Primality Test

The java 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.

Here the paramater of isProbablePrime() checks for the prime of this BigInteger based on a threshold value (1 – 1/2certainty).

Problem 

A prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. For example, the first six prime numbers are 2,3 ,5 ,7 ,11 , and 13 .

Given a large integer,n , use the Java BigInteger class' isProbablePrime method to determine and print whether it's prime or not prime.

Input Style

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

Constraints

n contains at most  digits.

Output Style

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

Sample Input

13

Sample Output

prime

Explanation

The only positive divisors of 13 are 1  and 13, so we print prime.





Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAPPY CODING 😁





Solution
( Java )


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

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        String n = scanner.nextLine();
       BigInteger num=new BigInteger(n);
       System.out.println(num.isProbablePrime(100)?"prime":"not prime");
        scanner.close();
    }
}


Post a Comment

0 Comments

Top Post Ad

Below Post Ad