-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.cpp
More file actions
160 lines (123 loc) · 4.49 KB
/
primes.cpp
File metadata and controls
160 lines (123 loc) · 4.49 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <primes.hpp>
#include <exponentiation.hpp>
#include <cassert>
#include <utility>
#include <functional>
namespace nm {
/* T is expected to be any unsigned or equivalent integer data type */
template<typename T>
std::vector<T> eratosthenes_sieve(const T n) {
std::vector<bool> is_prime(n + 1, true);
for (std::size_t i = 2; i <= n; i++) {
if (not is_prime[i]) continue;
for (std::size_t j = i * i; j <= n; j += i)
is_prime[j] = false;
}
std::vector<T> primes;
for (std::size_t i = 2; i <= n; i++)
if (is_prime[i])
primes.push_back(i);
return primes;
}
} // sieve
template std::vector<std::size_t> nm::eratosthenes_sieve<std::size_t>(std::size_t);
template std::vector<std::int32_t> nm::eratosthenes_sieve<std::int32_t>(std::int32_t);
template std::vector<std::int64_t> nm::eratosthenes_sieve<std::int64_t>(std::int64_t);
namespace nm {
Primality::Primality(std::uint32_t iterations) : i(iterations) {
if (i == 0) i = SMALLEST_PERFECT;
}
std::uint32_t Primality::trivial(std::uint32_t number) {
if (number < 2) return 0;
if (number < 4) return 1;
return number;
}
bool Primality::fermat(std::uint64_t number) {
if (this->trivial(number) < 2)
return this->trivial(number);
std::uint32_t iterations = this->i;
Random random;
while (iterations--) {
std::uint64_t x = random.number(2, number - 1);
if (mod_bin_exp_iterative<std::int64_t>(x, number - 1, number) != 1)
return false;
}
return true;
}
/*
* Miller-Rabin
* Test for non-compositeness.
* Number must fit in 32 bit unsigned integer.
*/
bool Primality::miller_rabin(std::uint32_t number) {
if (this->trivial(number) < 2)
return this->trivial(number);
std::uint32_t iterations = this->i;
std::uint64_t r = 0;
std::uint64_t d = number - 1;
while (not (d % 2)) {
d /= 2;
r++;
}
std::function<bool(std::uint64_t)> composite = [&] (std::uint64_t a) -> bool {
std::uint64_t b = mod_bin_exp_iterative<std::int64_t>(a, d, number);
if (b == 1 or b == number - 1) return false;
for (std::int64_t j = 0; j < r; j++) {
b = b * b % number;
if (b == number - 1)
return false;
}
return true;
};
while (iterations--) {
std::uint64_t a = this->random.number(2, number - 2);
if (composite(a)) return false;
}
return true;
}
/*
* Solovay–Strassen
*/
bool Primality::solovay_strassen(std::uint32_t number) {
if (this->trivial(number) < 2)
return this->trivial(number);
std::uint32_t iterations = this->i;
std::function<std::uint64_t(std::uint64_t, std::uint64_t)> legendre_jacobi =
[&] (std::uint64_t p, std::uint64_t q) -> std::uint64_t {
if (p == 0) return 0;
if (p == 1) return 1;
if (not (q & 1)) return 0;
std::int64_t r, symbol = 1;
const std::int64_t Q = 4;
while (p) {
while (not (p & 1)) {
p >>= 1;
r = q % (2 * Q);
if (r == Q - 1 or r == Q + 1)
symbol = -symbol;
}
std::swap(p, q);
if (p % Q == Q - 1 and q % Q == Q - 1)
symbol = -symbol;
p %= q;
}
if (not q) return 0;
return (symbol + number) % number;
};
while (iterations--) {
std::uint64_t a = this->random.number(2, number - 2);
std::uint64_t b = legendre_jacobi(a, number);
if (b == 0 or mod_bin_exp_iterative<std::int64_t>(a,
(number - 1) / 2, number) != b) return false;
}
return true;
}
/*
* Agarwal-Kayal-Saxena
* http://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf
*/
bool agarwal_kayal_saxena(std::uint64_t number) {
// TODO: non-generic practical implementation
return false;
}
} // primality