-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimality_test.cpp
62 lines (56 loc) · 1016 Bytes
/
primality_test.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
#include<bits/stdc++.h>
#define ull unsigned long long int
using namespace std;
ull binpower( ull a,ull b,ull p)
{
ull res=1;
a=a%p;
while(b>0){
if(b&1)
res*=a%p;
a=a*a%p;
b>>=1;
}
return res;
}
bool check_composite(ull n, ull a, ull d, int s) {
ull x = binpower(a, d, n);
if (x == 1 || x == n - 1)
return false;
for (int r = 1; r < s; r++) {
x = x * x % n;
if (x == n - 1)
return false;
}
return true;
};
bool MillerRabin(ull n, int iter=5) {
if (n < 4)
return n == 2 || n == 3;
int s = 0;
ull d = n - 1;
while ((d & 1) == 0) {
d >>= 1;
s++;
}
for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (n == a)
return true;
if (check_composite(n, a, d, s))
return false;
}
return true;
}
int main(){
int t;
ull n;
cin>>t;
while(t--){
cin>>n;
if(MillerRabin(n,5))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}