-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_if_prime.cpp
67 lines (52 loc) · 1.17 KB
/
check_if_prime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
For a given number N 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'. T testcases follow. Each test case should contain a positive integer N.
Output:
For each testcase, in a new line, print "Yes" if it is a prime number else print "No".
Constraints:
1 <= T <= 100
1 <= N <= 109
Example:
Input:
1
5
Output:
Yes
Explanation:
Testcase 1: 5 is the prime number as it is divisible only by 1 and 5.
*/
#include<bits/stdc++.h>
using namespace std;
bool prime(long int n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(long int i=5;i<=sqrt(n);i+=6){
if(n%i==0 || n%(i+2)==0) return false;
}
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
long int n;
cin>>n;
prime(n)? cout<<"Yes\n" : cout<<"No\n";
}
return 0;
}
/*
Prime number is in form of 6p+1, 6p-1
example
6p divisible by 6
6p+1
6p+2 divisible by 2
6p+3 divisible by 3
6p+4 divisible by 4
6p+5 = 6p+6-1 = 6(p+1)-1 = 6X-1
6p+6 divisible by 6
*/