[HackerRank] Java Primality Test
https://www.hackerrank.com/challenges/java-primality-test/problem?isFullScreen=true
isProbablePrime()
Returns true if this BigInteger is probably prime, false if itโs definitely composite. If certainty is โค 0, true is returned.
Parameters:
certainty - a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter.
Returns:
true if this BigInteger is probably prime, false if itโs definitely composite.
Solution
import java.util.*;
public class Solution {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println(scanner.nextBigInteger().isProbablePrime(100)
? "prime" : "not prime");
}
}
}
Leave a comment