Java Primality Test

  • + 9 comments

    My solution :

    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");
            }
        }
    }
    

    Now my question : the editorial suggests a value of 1 for the parameter of isProbablePrime, and many solution here suggests the same. I don't understand why.

    The doc of isProbablePrime says : "Parameter 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/2)^certainty). The execution time of this method is proportional to the value of this parameter."

    So it seems to me that a small value (1) would provide an important risk of false positive (the probability that the number tested is prime exceeds 1 - (1/2)^1 = 0.5 = 50%), and that using a bigger number as parameter would provide much more certainty ?

    Since here we're looking for precision and not speed, it seems far more safe to give the parameter a bigger value.

    In fact, I'm surprised that all the tests cases pass with a value of 1.