We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- BigNumber
- Java Primality Test
- Discussions
Java Primality Test
Java Primality Test
Sort by
recency
|
265 Discussions
|
Please Login in order to post a comment
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger;
public class PrimalityTest {
}
My takeaway here is to not trust the hackerrank editor's rendering of Javadocs... As viewed through hackerrank, the Javadoc for BigInteger.isProbablePrime(int) states that "if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty)", which certainly looks like it was intended to mean probability > 1 - (1/2) * certainty. However, viewing the Javadoc on Oracle's website makes it much more clear that what was actually meant was probability > 1 - (1/2) ^ certainty...
Considering the input type is String. So, first constructing a new object of BigInteger without referecing it to BigInteger type variable and then calling the suggested method on that.
System.out.println(new BigInteger(n).isProbablePrime(1) ? "prime" : "not prime");
I try this