-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
271 lines (237 loc) · 8.76 KB
/
Main.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
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
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <memory>
#include <vector>
#include <gmpxx.h>
#include "BoundedFactorizations.h"
#include "BoundedPrimeFixedSizeSets.h"
#include "BoundedPrimeSets.h"
#include "Factorization.h"
#include "FactorSieve.h"
#include "PrimeCount.h"
#include "PrimePower.h"
#include "PrimeSieve.h"
#include "PrimeTest.h"
int main ()
{
char c;
while (true)
{
std::cout
<< "1: Sieve\n"
<< "2: Factor\n"
<< "3: Count\n"
<< "4: Iterator\n"
<< "5: Factor Sieve\n"
<< "6: Test\n"
<< "7: Quit\n";
std::cin >> c;
std::cout << "\n";
if (c == '1')
{
std::uint64_t limit;
std::cout << "Limit: ";
std::cin >> limit;
std::cout << "\n";
PrimeSieve sieve (limit, true);
std::size_t count = sieve.Count ();
std::cout << "Found " << count << " primes less than " << limit << "\n\n";
while (true)
{
std::size_t index;
std::cout << "Index (-1 to return to menu): ";
std::cin >> index;
std::cout << "\n";
if (index == -1)
break;
else if (index >= count)
std::cout << "Index too large\n\n";
else
{
for (std::size_t i = index; i < index + 10 && i < count; ++i)
std::cout << (*sieve.Primes ())[i] << "\n";
std::cout << "\n";
}
}
}
else if (c == '2')
{
std::uint64_t n;
std::cout << "n: ";
std::cin >> n;
std::cout << "\n";
Factorization factorization (n, true);
if (factorization.IsPrime ())
std::cout << n << " is prime\n";
else
{
std::cout << "Prime factors of " << n << "\n\n";
for (const auto& primePower : *factorization.PrimeFactors ())
std::cout << primePower.prime << "^" << primePower.power << "\n";
std::cout << "\nFactors of " << n << "\n\n";
for (std::uint64_t factor : *factorization.Factors ())
std::cout << factor << "\n";
}
std::cout
<< "\nomega(n): " << factorization.SmallOmega ()
<< "\nOmega(n): " << factorization.BigOmega ()
<< "\ntau(n): " << factorization.Tau ()
<< "\nSum of proper factors of n: " << factorization.SumProperFactors ()
<< "\nsigma1(n): " << factorization.Sigma1 ()
<< "\nmu(n): " << factorization.Mu ()
<< "\nlambda(n): " << factorization.SmallLambda ()
<< "\nRadical of n: " << factorization.Radical ()
<< "\nphi(n): " << factorization.EulerPhi ()
<< "\nCarmichael function of n: " << factorization.CarmichaelFunction ()
<< "\n\n";
if (factorization.IsPerfect ())
std::cout << n << " is perfect\n\n";
else if (factorization.IsDeficient ())
std::cout << n << " is deficient\n\n";
else
std::cout << n << " is abundant\n\n";
}
else if (c == '3')
{
std::uint64_t n;
std::cout << "n: ";
std::cin >> n;
std::cout << "\n";
PrimeSieve sieve (n, false);
std::cout
<< "Found " << sieve.Count () << " primes less than " << n << "\n"
<< "Legendre estimate: " << LegendreCount (n) << " primes less than " << n << "\n"
<< "Logarithmic integral estimate: " << LiCount (n) << " primes less than " << n << "\n\n";
}
else if (c == '4')
{
std::cout
<< "1: Prime Sets\n"
<< "2: Fixed-Size Prime Sets\n"
<< "3: Factorizations\n";
std::cin >> c;
std::cout << "\n";
if (c == '1')
{
std::uint64_t limit;
std::cout << "Limit: ";
std::cin >> limit;
std::cout
<< "\n"
<< "1 = (empty product)\n"
<< "mu(1) = 1\n";
BoundedPrimeSetIterator bpsi (limit);
std::size_t counter = 1;
for (++bpsi; !bpsi.IsEnd (); ++bpsi)
{
auto primes = bpsi.Primes ();
std::cout << bpsi.N () << " = " << (*primes)[0];
for (auto prime = primes->cbegin () + 1; prime != primes->cend (); ++prime)
std::cout << " * " << *prime;
std::cout
<< "\n"
<< "mu(" << bpsi.N () << ") = " << bpsi.MoebiusN () << "\n";
++counter;
}
std::cout
<< "\n"
<< "Counted " << counter << " prime sets.\n\n";
}
else if (c == '2')
{
std::uint64_t limit;
std::cout << "Limit: ";
std::cin >> limit;
std::cout << "\n";
std::uint32_t setSize;
std::cout << "Set size: ";
std::cin >> setSize;
std::cout << "\n";
if (setSize == 0)
std::cout << "1 = (empty product)\n\n";
else
{
BoundedPrimeFixedSizeSetIterator bpfssi (limit, setSize);
std::size_t counter = 0;
for (; !bpfssi.IsEnd (); ++bpfssi)
{
auto primes = bpfssi.Primes ();
std::cout << bpfssi.N () << " = " << (*primes)[0];
for (auto prime = primes->cbegin () + 1; prime != primes->cend (); ++prime)
std::cout << " * " << *prime;
std::cout << "\n";
++counter;
}
std::cout
<< "\n"
<< "Counted " << counter << " fixed-size prime sets.\n\n";
}
}
else if (c == '3')
{
std::uint64_t limit;
std::cout << "Limit: ";
std::cin >> limit;
std::cout << "\n";
BoundedFactorizationIterator bfi (limit);
std::cout << "1 = (empty product)\n";
std::size_t counter = 1;
for (++bfi; !bfi.IsEnd (); ++bfi)
{
auto factorization = bfi.Factorization ();
std::cout << bfi.N () << " = " << (*factorization)[0].prime;
if ((*factorization)[0].power > 1)
std::cout << "^" << (*factorization)[0].power;
for (auto primePower = factorization->cbegin () + 1;
primePower != factorization->cend ();
++primePower)
{
std::cout << " * " << primePower->prime;
if (primePower->power > 1)
std::cout << "^" << primePower->power;
}
std::cout
<< "\n"
<< "mu(" << bfi.N () << ") = " << bfi.MoebiusN () << "\n";
++counter;
}
std::cout
<< "\n"
<< "Counted " << counter << " factorizations.\n\n";
}
else
std::cout << "Bad option " << c << "\n\n";
}
else if (c == '5')
{
std::uint64_t limit;
std::cout << "Limit: ";
std::cin >> limit;
std::cout << "\n";
FactorSieve sieve (limit);
for (std::uint64_t n = 0; n < limit; ++n)
std::cout << n << ": " << sieve.LeastPrimeFactor (n) << "\n";
std::cout << "\n";
}
else if (c == '6')
{
mpz_class n;
std::cout << "n: ";
std::cin >> n;
mpz_class base;
std::cout << "Base: ";
std::cin >> base;
std::cout << n;
if (MillerRabinProbabilisticTest (n, base))
std::cout << " is probably prime.\n\n";
else
std::cout << " is composite.\n\n";
}
else if (c == '7')
break;
else
std::cout << "Bad option: " << c << "\n\n";
}
return 0;
}