Skip to main content

Posts

Showing posts with the label Fermat's theorem

Check a number is prime or not [BASIC]

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: 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. Output: Print "Yes" if it is a prime number else print "No". Constraints: 1<= T <=30 1<= N <=100 Example:  Input: 1 5 Output: 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();    ...