I am using Fermat's Theorem which isn't applicable on few numbers known as Carmichael number so do look carefully.
Fermat's theorem states that if I take any arbitrary number "a" and the number to test is p then, p is prime if:
(a^p-a%a==0)
Question:
Output:
Print "Yes" if it is a prime number else print "No".
Constraints:
1<= T <=30
1<= N <=100
Example:
Fermat's theorem states that if I take any arbitrary number "a" and the number to test is p then, p is prime if:
(a^p-a%a==0)
Question:
For a given number check if it is prime or not. A prime number is a number which is only divisible by 1 and itself.
Input:
First line contains an integer, the number of test cases 'T'. Each test case should contain a positive integer N.
First line contains an integer, the number of test cases 'T'. Each test case should contain a positive integer N.
Output:
Print "Yes" if it is a prime number else print "No".
Constraints:
1<= T <=30
1<= N <=100
Example:
Input:
1
5
1
5
Output:
Yes
Yes
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n =sc.nextInt();
if((Math.pow(2,n)-2)%n==0){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
EXECUTION TIME: 0.11 sec
Comments
Post a Comment