Java Program to Implement Miller Rabin Primality Test

«
»
This is a Java Program to Implement Miller Rabin Primality Test Algorithm. Miller Rabin Primality Test is an algorithm which is used to determine if a given number is prime or not.

Here is the source code of the Java Program to Implement Miller Rabin Primality Test Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to Implement Miller Rabin Primality Test Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.Random;
  7. import java.math.BigInteger;
  8.  
  9. /** Class MillerRabin **/
  10. public class MillerRabin
  11. {
  12.     /** Function to check if prime or not **/
  13.     public boolean isPrime(long n, int iteration)
  14.     {
  15.         /** base case **/
  16.         if (n == 0 || n == 1)
  17.             return false;
  18.         /** base case - 2 is prime **/
  19.         if (n == 2)
  20.             return true;
  21.         /** an even number other than 2 is composite **/
  22.         if (n % 2 == 0)
  23.             return false;
  24.  
  25.         long s = n - 1;
  26.         while (s % 2 == 0)
  27.             s /= 2;
  28.  
  29.         Random rand = new Random();
  30.         for (int i = 0; i < iteration; i++)
  31.         {
  32.             long r = Math.abs(rand.nextLong());            
  33.             long a = r % (n - 1) + 1, temp = s;
  34.             long mod = modPow(a, temp, n);
  35.             while (temp != n - 1 && mod != 1 && mod != n - 1)
  36.             {
  37.                 mod = mulMod(mod, mod, n);
  38.                 temp *= 2;
  39.             }
  40.             if (mod != n - 1 && temp % 2 == 0)
  41.                 return false;
  42.         }
  43.         return true;        
  44.     }
  45.     /** Function to calculate (a ^ b) % c **/
  46.     public long modPow(long a, long b, long c)
  47.     {
  48.         long res = 1;
  49.         for (int i = 0; i < b; i++)
  50.         {
  51.             res *= a;
  52.             res %= c; 
  53.         }
  54.         return res % c;
  55.     }
  56.     /** Function to calculate (a * b) % c **/
  57.     public long mulMod(long a, long b, long mod) 
  58.     {
  59.         return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).mod(BigInteger.valueOf(mod)).longValue();
  60.     }
  61.     /** Main function **/
  62.     public static void main (String[] args) 
  63.     {
  64.         Scanner scan = new Scanner(System.in);
  65.         System.out.println("Miller Rabin Primality Algorithm Test\n");
  66.         /** Make an object of MillerRabin class **/
  67.         MillerRabin mr = new MillerRabin();
  68.         /** Accept number **/
  69.         System.out.println("Enter number\n");
  70.         long num = scan.nextLong();
  71.         /** Accept number of iterations **/
  72.         System.out.println("\nEnter number of iterations");
  73.         int k = scan.nextInt();
  74.         /** check if prime **/
  75.         boolean prime = mr.isPrime(num, k);
  76.         if (prime)
  77.             System.out.println("\n"+ num +" is prime");
  78.         else
  79.             System.out.println("\n"+ num +" is composite");
  80.  
  81.     }
  82. }

advertisement
Miller Rabin Primality Algorithm Test
 
Enter number
 
5510389
 
Enter number of iterations
2
 
5510389 is prime

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.