Jump to content

Miller–Rabin primality test: Difference between revisions

Added Java implementation
(Added Java implementation)
Line 654:
=={{header|J}}==
See [[j:Essays/Primality%20Tests#Miller-Rabin|Primality Tests essay on the J wiki]].
 
=={{header|Java}}==
 
The Miller-Rabin primality test is part of the standard library (java.math.BigInteger)
 
<lang java>import java.math.BigInteger;
 
public class MillerRabinPrimalityTest
{
public static void main(String[] args)
{
BigInteger n = new BigInteger(args[0]);
int certainty = Integer.parseInt(args[1]);
System.out.println(n.toString() + " is " + (n.isProbablePrime(certainty) ? "probably prime" : "composite"));
}
}</lang>
 
Sample output:
 
<pre>java MillerRabinPrimalityTest 123456791234567891234567 1000000
123456791234567891234567 is probably prime</pre>
 
=={{header|PARI/GP}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.