Posts

Search

Primality Testing - IV

Prime numbers are an essential part of cryptography. Some cryptographic algorithms such as RSA critically depend on the fact that prime factorization of large numbers takes a long time. Therefore, it is important to identify large prime numbers. One such algorithm, which can identify if a given number is prime, is Miller–Rabin primality test.
Your task is to implement this primality test and check if a number is prime.

Input format
The first line contains the number of test cases, T.
Next T lines contain a number.
Output Format
In each of T lines for each prime numbers, print “Yes” if the number is prime and “No” otherwise.
Sample Input 0
3
32416190071 
15487469
15487473
Sample Output 0
Yes
Yes
No
SOLUTION :-

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    static void bestMethod(Long n){
        boolean isPrime = true;
        for (int i = 2; i <=Math.sqrt(n) ; i++) {
            if(n%i==0) {
                System.out.println("No");
                isPrime = false;
                break;
            }
        }
        if(isPrime)
            System.out.println("Yes");

    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        long m,n;
        m=sc.nextLong();
        for(long i=0;i<m;i++)
        {
            n=sc.nextLong();
            bestMethod(n);
        }
       
    }

}