-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiller-Rabin-primarilty-test.cpp
More file actions
60 lines (55 loc) · 1.06 KB
/
Miller-Rabin-primarilty-test.cpp
File metadata and controls
60 lines (55 loc) · 1.06 KB
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
uint64_t modmul(uint64_t a, uint64_t b, uint64_t mod){
uint64_t res = 0;
a %= mod;
while (b > 0){
if (b & 1)
res = (res + a) % mod;
a = (2 * a) % mod;
b /= 2LL;
}
return res % mod;
}
uint64_t binpower(uint64_t base, uint64_t e, uint64_t mod) {
uint64_t result = 1;
base %= mod;
while (e) {
if (e & 1)
result = modmul(result, base, mod);
base = modmul(base, base, mod);
e >>= 1;
}
return result;
}
bool check_composite(uint64_t N, uint64_t A, uint64_t D, int R) {
uint64_t x = binpower(A, D, N);
if (x == 1 || x == N - 1) {
return false;
}
for (int r = 1; r < R; r++) {
x = modmul(x, x, N);
if (x == N - 1) {
return false;
}
}
return true;
};
bool MillerRabin(uint64_t N) {
if (N < 2) {
return false;
}
int64_t D = N - 1;
int R = 0;
while ((D & 1) == 0) {
D >>= 1;
R++;
}
for (uint64_t A : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (N == A) {
return true;
}
if (check_composite(N, A, D, R)) {
return false;
}
}
return true;
}