-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.cc
More file actions
343 lines (282 loc) · 6.67 KB
/
prime.cc
File metadata and controls
343 lines (282 loc) · 6.67 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Primality testing. Carmichael numbers.
#include <concepts>
#include <cstdlib>
#include <initializer_list>
#include <numeric>
#define assert(X) do { if (!(X)) std::abort (); } while(0)
/* True iff N is prime. */
static bool
prime_p (int n)
{
if (n <= 3)
return n > 1;
else if ((n % 2) == 0 || (n % 3) == 0)
return false;
int i = 5;
while (i * i <= n)
{
if ((n % i) == 0 || n % (i + 2) == 0)
return false;
i += 6;
}
return true;
}
/* True iff A and B are coprimes. */
static bool
coprime_p (int a, int b)
{
return std::gcd (a, b) == 1;
}
/* Return true iff N is divisible by I. */
template<std::integral I>
bool divides (I i, I n)
{
return n % i == 0;
}
/* Returns true iff N is even. */
template<std::integral I>
bool even (I n)
{
return !(n & 1);
}
/* Returns true iff N is odd. */
template<std::integral I>
bool odd (I n)
{
return n & 1;
}
/* Halve N. */
template<std::integral I>
I half (I n)
{
return n >> 1;
}
/* Return the smallest divisor of N. N > 0. */
template<std::integral I>
I smallest_divisor (I n)
{
if (even (n))
return 2;
for (I i = 3; i * i <= n; i+= 2)
if (divides (i, n))
return i;
return n;
}
/* True iff N is prime. */
template<std::integral I>
bool is_prime (I n)
{
return n > 1 && smallest_divisor (n) == n;
}
/* The above is slow -- it's exponential in the number of digits. Here's
a different approach. */
template<std::integral I>
struct modulo_multiply {
I modulus;
modulo_multiply(const I& i) : modulus(i) {}
I operator() (const I& n, const I& m) const {
return (n * m) % modulus;
}
};
template<std::integral I>
I identity_element (const modulo_multiply<I> &)
{
return 1;
}
/* For now. */
template<typename>
concept regular_type = true;
template<typename>
concept monoid_op = true;
template<typename>
concept semigroup_op = true;
template<regular_type A, std::integral N, semigroup_op Op>
A power_accumulate_semigroup (A r, A a, N n, Op op)
{
if (n == 0)
return r;
assert (n > 0);
while (true)
{
if (odd (n))
{
r = op (r, a);
if (n == 1)
return r;
}
n = half (n);
a = op (a, a);
}
}
template<regular_type A, std::integral N, semigroup_op Op>
A power_semigroup (A a, N n, Op op)
{
assert (n > 0);
while (!odd (n))
{
a = op (a, a);
n = half (n);
}
if (n == 1)
return a;
return power_accumulate_semigroup (a, op (a, a), half (n - 1), op);
}
template<regular_type A, std::integral N, monoid_op Op>
A power_monoid (A a, N n, Op op)
{
if (n == 0)
return identity_element (op);
assert (n > 0);
return power_semigroup (a, n, op);
}
/* We want to compute a multiplicative inverse modulo prime p.
Fermat: the inverse of a, where 0 < a < p is a^{p - 2}.
(Inverse WRT p means having a remainder of 1 after dividing by p.
P is prime, A > 0. */
template<std::integral I>
I multiplicative_inverse_fermat (I a, I p)
{
return power_monoid (a, p - 2, modulo_multiply<I>(p));
}
/* Fermat's Little Theorem:
If p is prime, then a^{p - 1} - 1 is divisible by p for any 0 < a < p.
which means:
If p is prime, then a^{p - 1} = 1 mod p for any 0 < a < p.
We want to know if N is prime. Take an arbitrary number a smaller than n,
raise it to the n - 1 power and check if the result is 1. If not, n is
*not* prime. If it is 1, there's a chance that n is prime. Try random a. */
template<std::integral I>
bool fermat_test (I n, I a)
{
assert (a > 0 && a < n);
I r = power_semigroup (a, n - 1, modulo_multiply<I>(n));
return r == 1;
}
/* Try out the above. */
template<std::integral I>
bool try_it (I n)
{
if (!fermat_test (n, n - 1))
return false;
if (!fermat_test (n, n / 2))
return false;
if (!fermat_test (n, n / 3))
return false;
if (!fermat_test (n, n / 4))
return false;
/* Assume N is prime. */
return true;
}
/* The above will be fooled by Carmichael numbers! If we try a's coprime
to N. Try for 172081, a Carmichael number, with prime factorization
7 * 13 * 31 * 61. */
static void
try_carm ()
{
constexpr int n = 172081;
// NB: larger numbers would cause an overflow.
for (auto i : { 9, 81, 123 })
{
if (coprime_p (n, i) && fermat_test (n, i))
/* Fooled as expected. */;
else
assert (!"huh?");
}
}
/* Modular exponentiation using exponentiation by squaring. B is the base,
E the exponent, and M the modulus. */
template<std::integral I>
I modular_pow (I b, I e, I m)
{
if (m == 1)
return 0;
I r = 1;
b %= m;
while (e > 0)
{
if (odd (e))
r = (r * b) % m;
e >>= 1;
b = (b * b) % m;
}
return r;
}
/* True iff N is a Carmichael number.
A composite number n > 1 is a Carmichael number iff
for each b > 1, coprime(b, n) => b^{n - 1} = 1 mod n
Here, b^{n - 1} may be an extremely large number, so we have to use
modular exponentiation. */
template<std::integral I>
bool is_carmichael (I n)
{
if (n <= 1 || is_prime (n))
return false;
for (I b = 2; b < n; ++b)
if (coprime_p (b, n))
{
I r = modular_pow (b, n - 1, n);
if ((r % n) != 1)
return false;
}
return true;
}
/* Find the first N Carmichael numbers. */
static void
find_first_n_carmichaels (int n)
{
__builtin_printf ("First %d Carmichael numbers:", n);
for (int i = 2; i < std::numeric_limits<int>::max() && n > 0; ++i)
if (is_carmichael (i))
{
__builtin_printf (" %d", i);
--n;
}
__builtin_printf ("\n");
}
/* Return true if N is probably prime, and false if it definitely
is not. Uses the Miller-Rabin test. */
template<std::integral I>
bool miller_rabin_test (I n, I q, I k, I w)
{
/* Assume n > 1 && n - 1 = 2^k * q && odd (q) */
modulo_multiply<I> mmult (n);
I x = power_semigroup (w, q, mmult);
if (x == 1 || x == n - 1)
return true;
for (I i = 1; i < k; ++i)
{
x = mmult (x, x);
if (x == n - 1)
return true;
if (x == 1)
return false;
}
return false;
}
int
main ()
{
assert (prime_p (7753));
assert (!prime_p (7791));
assert (is_prime (7753));
assert (!is_prime (7791));
assert (try_it (11));
assert (!try_it (12));
assert (try_it (7753));
assert (!try_it (7791));
/* Let's try a Carmichael number (which is not prime). */
assert (!prime_p (172081));
assert (!is_prime (172081));
try_carm ();
assert (modular_pow (5, 3, 13) == 8);
assert (modular_pow (4, 13, 497) == 445);
assert (is_carmichael (172081L));
assert (!is_carmichael (7753));
assert (!is_carmichael (7741));
assert (!is_carmichael (560));
assert (is_carmichael (561));
assert (is_carmichael (1105));
find_first_n_carmichaels (7);
assert (!miller_rabin_test (2793, 349, 3, 150));
assert (!miller_rabin_test (561, 35, 4, 7));
}