Question

In: Computer Science

Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer...

Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer is a prime. The input of the algorithm is a large positive integer. The output is “the number *** is a prime” or “the number *** is not a prime”. The error probability of the algorithm should be no more than 1 256 . Use this program to test some big integers. In Java, there is a class BigInteger. You can use methods of that class except the method isProbablePrime. write Java program.

Solutions

Expert Solution

CODE IN JAVA:

MillerRobin.java file:

import java.io.*;

import java.math.*;

import java.util.Scanner;

public class MillerRobin {

static int power(int a, int b, int p) {

  

int res = 1;

a = a % p;

  

while (b > 0) {

if ((b & 1) == 1)

res = (res * b) % p;

b = b >> 1;

a = (a * a) % p;

}

  

return res;

}

static boolean miillerTest(int d, int n) {

int a = 2 + (int)(Math.random() % (n - 4));

int x = power(a, d, n);

  

if (x == 1 || x == n - 1)

return true;

while (d != n - 1) {

x = (x * x) % n;

d *= 2;

if (x == 1)

return false;

if (x == n - 1)

return true;

}

return false;

}

static boolean isPrime(int n, int k) {

if (n <= 1 || n == 4)

return false;

if (n <= 3)

return true;

  

int d = n - 1;

  

while (d % 2 == 0)

d /= 2;

for (int i = 0; i < k; i++)

if (!miillerTest(d, n))

return false;

  

return true;

}

public static void main(String[] args) {

int flag = 4 ;

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number:");

int number = sc.nextInt();

if(isPrime(number,flag))

System.out.println(number+" is prime number");

else

System.out.println(number+" is not a prime number");

}

}

OUTPUT:


Related Solutions

Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Describe a non-deterministic Turing machine algorithm that takes one integer and checks for primality. What is...
Describe a non-deterministic Turing machine algorithm that takes one integer and checks for primality. What is the complexity of the algorithm?
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT