-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiller–Rabin primality test.cpp
More file actions
129 lines (95 loc) · 2.96 KB
/
Miller–Rabin primality test.cpp
File metadata and controls
129 lines (95 loc) · 2.96 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Miller–Rabin primality test
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef unsigned long long ULL;
ULL modular_mul(ULL a, ULL b, ULL modulus)
{
ULL result = 0;
a %= modulus;
while(b > 0) {
if(b & 1) {
result = (result + a) % modulus;
}
a = (a << 1) % modulus; // a = (a * 2) % modulus.
b >>= 1; // b /= 2.
}
return result % modulus;
}
ULL modular_pow(ULL base, ULL exponent, ULL modulus)
{
if(modulus == 1) return 0;
ULL result = 1;
base %= modulus;
while(exponent > 0) {
if(exponent & 1) {
result = modular_mul(result, base, modulus);
}
exponent >>= 1;
base = modular_mul(base, base, modulus);
}
return result;
}
bool isPrime(ULL n, int iteration)
{
if(n < 2) return false;
if(n <= 3) return true;
if(!(n & 1)) return false; // If n is even.
ULL d = n-1; // d * 2^r = n-1 where d is odd and r > 0.
while(!(d & 1))
d >>= 1;
while(iteration--) { // Witness loop.
ULL a = rand() % (n-1) + 1;
ULL x = modular_pow(a, d, n); // x = a^d % n
ULL d1 = d;
while(d1 != n-1 && x != 1 && x != n-1) {
x = modular_mul(x, x, n); // x = (x * x) % n.
d1 <<= 1;
}
if(x != n-1 && !(d1 & 1)) {
return false;
}
}
return true;
}
int main()
{
ULL n;
while(scanf("%llu", &n) != EOF)
(isPrime(n, 5))? puts("Yes") : puts("No");
return 0;
}
// The above isPrime() can be replaced with the function below.
// The calculation is same but it is more verbose.
/*
bool isPrime(ULL n, int iteration)
{
if(n < 2) return false;
if(n <= 3) return true;
if(!(n & 1)) return false; // If n is even.
ULL d = n-1, r = 0;
while(!(d & 1)) { // Calculate d, r
d >>= 1; // Such that d * 2^r = n-1 where d is odd and r > 0.
++r;
}
while(iteration--) { // Witness loop.
ULL a = rand() % (n-3) + 2; // Random number [2, n-2]
ULL x = modular_pow(a, d, n); // x = a^d % n
if(x == 1 or x == n-1) continue; // So, after repeated square, a^(n-1) = 1 (mod n)
bool continue_witness_loop = false;
for(int i = 1; i < r; ++i) { // Loop r-1 times.
x = modular_mul(x, x, n); // x = (x * x) % n.
if(x == 1) {
return false; // Composite.
}
else if(x == n-1) {
continue_witness_loop = true; // So, after repeated square, a^(n-1) = 1 (mod n)
break;
}
}
if(continue_witness_loop) continue;
return false; // Composite.
}
return true; // Probably prime
}
*/