-
Notifications
You must be signed in to change notification settings - Fork 1
/
gap_common.cpp
1360 lines (1112 loc) · 43.5 KB
/
gap_common.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Seth Troisi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gap_common.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <limits>
/* for dirname(3) */
#include <libgen.h>
/* for primesieve::iterator */
#include <primesieve.hpp>
using std::cout;
using std::endl;
using std::pair;
using std::string;
using std::vector;
using namespace std::chrono;
string UNKNOWNS_DIR = "unknowns";
static const std::map<uint64_t,uint64_t> common_primepi = {
{ 10'000'000, 664'579},
{ 100'000'000, 5'761'455},
{ 200'000'000, 11'078'937},
{ 400'000'000, 21'336'326},
{ 800'000'000, 41'146'179},
{ 1'000'000'000, 50'847'534},
{ 2'000'000'000, 98'222'287},
{ 3'000'000'000, 144'449'537},
{ 4'000'000'000, 189'961'812},
{ 5'000'000'000, 234'954'223},
{ 6'000'000'000, 279'545'368},
{ 10'000'000'000, 455'052'511},
{ 15'000'000'000, 670'180'516},
{ 20'000'000'000, 882'206'716},
{ 25'000'000'000, 1'091'987'405},
{ 30'000'000'000, 1'300'005'926},
{ 40'000'000'000, 1'711'955'433},
{ 50'000'000'000, 2'119'654'578},
{ 60'000'000'000, 2'524'038'155},
{ 100'000'000'000, 4'118'054'813},
{ 200'000'000'000, 8'007'105'059},
{ 300'000'000'000, 11'818'439'135},
{ 400'000'000'000, 15'581'005'657},
{ 500'000'000'000, 19'308'136'142},
{1'000'000'000'000, 37'607'912'018},
{2'000'000'000'000, 73'301'896'139},
{3'000'000'000'000, 108'340'298'703},
{4'000'000'000'000, 142'966'208'126},
{5'000'000'000'000, 177'291'661'649}
};
static void assert_file_exists(string path) {
std::ifstream f(path);
if (!f.good()) {
printf("'%s' doesn't exist\n", path.c_str());
exit(1);
}
}
bool has_prev_prime_gmp() {
return (
(__GNU_MP_VERSION > 6) ||
(__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR > 3) ||
(__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR == 2 && __GNU_MP_VERSION_PATCHLEVEL == 99)
);
}
uint64_t gcd(uint64_t a, uint64_t b) {
if (b == 0) return a;
return gcd(b, a % b);
}
double _log(const mpz_t &K) {
long exp;
double mantis = mpz_get_d_2exp(&exp, K);
return log(mantis) + log(2) * exp;
}
double calc_log_K(const struct Config& config) {
mpz_t K;
init_K(config, K);
double log = _log(K);
mpz_clear(K);
return log;
}
void init_K(const struct Config& config, mpz_t &K) {
mpz_init(K);
mpz_primorial_ui(K, config.p);
assert(0 == mpz_tdiv_q_ui(K, K, config.d));
assert(mpz_cmp_ui(K, 1) > 0); // K <= 1 ?!?
}
void K_stats(
const struct Config& config,
mpz_t &K, int *K_digits, double *K_log) {
init_K(config, K);
*K_log = _log(K);
if (K_digits != nullptr) {
int base10 = mpz_sizeinbase(K, 10);
*K_digits = base10;
if (config.verbose >= 2) {
int K_bits = mpz_sizeinbase(K, 2);
printf("K = %d bits, %d digits, log(K) = %.2f\n",
K_bits, base10, *K_log);
}
}
}
/**
* Return estimated time (in seconds) to PRP test a composite with no small factor
*/
double prp_time_estimate_composite(double K_log, int verbose) {
// TODO: For large K_log, time smaller PRP then upscale with polynomial
// Some rough estimates at
// https://github.com/sethtroisi/misc-scripts/tree/master/prime-time
float K_log_2 = K_log * K_log;
float t_estimate_poly = -1.1971e-03
+ 5.1072e-07 * K_log
+ 9.4362e-10 * K_log_2
+ 1.8757e-13 * K_log_2 * K_log
+ -1.9582e-18 * K_log_2 * K_log_2;
float t_estimate = std::max(1e-3f, t_estimate_poly);
if (verbose >= 2) {
if (t_estimate > 0.3) {
printf("Estimated secs/PRP: %.1f\n", t_estimate);
} else {
// Benchmark in thread
// Create some non-trivial semi-primes.
mpz_t n, p, q;
mpz_inits(n, p, q, nullptr);
size_t bits = K_log * 1.442;
assert( bits > 50 );
mpz_set_ui(n, 1);
// Multiply "large" static primes (25 bits+) to get number of size N
size_t bit_goal = bits - 24;
while (bit_goal > 0) {
// Important to not ever choose small p
size_t p_size = bit_goal < 50 ? bit_goal : 25;
assert(p_size >= 25);
mpz_ui_pow_ui(p, 2, p_size);
mpz_nextprime(p, p);
mpz_mul(n, n, p);
bit_goal -= p_size;
}
mpz_set(p, n);
// Smaller prime for fast nextprime.
// Large enough to avoid being found with trial division.
mpz_ui_pow_ui(q, 2, 25);
mpz_nextprime(q, q);
double t = 0;
size_t count = 0;
// time a reasonable number (or for 5 seconds)
for (; count < 15 || t < 5; count++) {
mpz_mul(n, p, q);
assert( mpz_sizeinbase(n, 2) >= bits );
auto s_start_t = high_resolution_clock::now();
assert( mpz_probab_prime_p(n, 25) == 0 );
t += duration<double>(high_resolution_clock::now() - s_start_t).count();
mpz_nextprime(q, q);
}
printf("Estimating PRP/s: %ld / %.2f = %.1f/s vs polyfit estimate of %.1f/s\n",
count, t, count / t, 1 / t_estimate);
t_estimate = t / count;
mpz_clears(n, p, q, nullptr);
}
}
return t_estimate;
}
// See misc/benchmark.cpp
static double benchmark_primorial_modulo(const mpz_t& K, size_t count) {
auto t_start = high_resolution_clock::now();
uint64_t z = 0;
// Benchmark suggest this doesn't really depend on size but use 34 bits
// As this is size of "most" of primes (and > 32)
uint64_t p = 1LL << 34;
for (size_t i = 0; i < count; i++) {
z += mpz_fdiv_ui(K, p + i);
}
double time = duration<double>(high_resolution_clock::now() - t_start).count();
// Keep compiler from optimizing out loop.
double eps = 1e-100 * z;
// printf("\tK mod / s Estimated %ld/%.2g = %.2g\n", count, time, count / time);
return time / count + eps;
}
/**
* Count of numbers [0, SL] coprime to K
*/
static
size_t count_coprime_sieve(const struct Config& config) {
vector<char> is_coprime(config.sieve_length + 1, true);
for (auto prime : get_sieve_primes(config.p)) {
if (config.d % prime == 0)
continue;
for (size_t i = 0; i < is_coprime.size(); i += prime)
is_coprime[i] = false;
}
return std::count(is_coprime.begin(), is_coprime.end(), true);
}
/**
* Count of numbers coprime to d less than end; sum( gcd(m, d) == 1 for m in range(n, n+i) )
* Uses inclusion exclusion on prime factorization of d
*/
static
uint64_t _r_count_num_m(uint64_t n, const vector<int> &factors_d, int i) {
if (n == 0) return 0;
if (i < 0) return n;
return _r_count_num_m(n, factors_d, i-1) - _r_count_num_m(n / factors_d[i], factors_d, i-1);
}
/**
* Count number of m [ms, ms + mi) coprime to d
*/
size_t count_num_m(long ms, long mi, uint64_t d) {
if (d == 1)
return mi;
if (ms + mi < 10000) {
size_t count = 0;
for (long m = ms; m < ms + mi; m++)
count += (gcd(m, d) == 1);
return count;
}
vector<int> D_factors;
{
uint64_t temp = d;
for (long p = 2; p*p <= temp; p++) {
while (temp % p == 0) {
D_factors.push_back(p);
temp /= p;
}
}
if (temp > 1)
D_factors.push_back(temp);
}
return _r_count_num_m(ms + mi - 1, D_factors, D_factors.size()-1) -
_r_count_num_m(ms - 1, D_factors, D_factors.size()-1);
}
/**
* Vector of mi, such that gcd(config.mstart + mi, config.d)
* Returns a copy, but copy is "fast" compared to cost of computing vector
*/
pair<vector<bool>, vector<uint32_t>> is_coprime_and_valid_m(const struct Config& config) {
const uint64_t M_start = config.mstart;
const uint64_t M_inc = config.minc;
assert(M_inc < std::numeric_limits<uint32_t>::max());
const uint32_t D = config.d;
const vector<uint32_t> P_primes = get_sieve_primes(config.p);
assert( P_primes.back() == config.p);
vector<uint32_t> valid_mi;
vector<bool> is_m_coprime(M_inc, 1);
for (uint32_t p : P_primes) {
if (D % p == 0) {
// mark off any m = m_start + mi that shares factor with d
uint64_t first = (p - (M_start % p)) % p;
assert((M_start + first) % p == 0);
for (uint64_t mi = first; mi < M_inc; mi += p) {
is_m_coprime[mi] = 0;
}
}
}
// Slower than dynamic bitset, but fast enough
size_t count = std::count(is_m_coprime.begin(), is_m_coprime.end(), 1);
valid_mi.reserve(count);
for (uint32_t mi = 0; mi < M_inc; mi++) {
if (is_m_coprime[mi]) {
assert(gcd(M_start + mi, D) == 1);
valid_mi.push_back(mi);
}
}
return {is_m_coprime, valid_mi};
}
pair<uint64_t, uint64_t> calculate_thresholds_method2(
const struct Config config,
size_t count_coprime_sieve,
size_t valid_ms) {
uint32_t sieve_interval = 2 * config.sieve_length + 1;
// (small vs modulo_search) MULT vs log2(MULT) * (M_inc/valid_ms)
float SMALL_MULT = std::max(8.0, log(8) * config.minc / valid_ms);
// (small vs medium) valid_m vs count_coprime_sieve * (M_inc / prime)
uint64_t MEDIUM_CROSSOVER_SMALL = 1.0 * count_coprime_sieve * config.minc / valid_ms;
// (medium vs modulo_search) count_coprime_sieve vs M*S/P * (log2(P) - log2(SL))
float M_PER_P_CROSSOVER = 1.0 * config.minc * sieve_interval / count_coprime_sieve;
// correct for how much work it takes to skip to next m
float MEDIUM_MULT = std::max(1.9, 0.65 * log2(M_PER_P_CROSSOVER / count_coprime_sieve));
uint64_t MEDIUM_CROSSOVER_SEARCH = MEDIUM_MULT * M_PER_P_CROSSOVER;
// XXX: What would it look like to do this more dynamically?
// Everytime prime >= next_mult run a couple through both MEDIUM & LARGE prime and choose faster.
uint64_t SMALL_THRESHOLD = std::min((uint64_t) SMALL_MULT * sieve_interval, MEDIUM_CROSSOVER_SMALL);
if (SMALL_THRESHOLD < sieve_interval) {
SMALL_THRESHOLD = sieve_interval + 1;
}
uint64_t MEDIUM_THRESHOLD = std::max(SMALL_THRESHOLD, MEDIUM_CROSSOVER_SEARCH);
MEDIUM_THRESHOLD = std::min(MEDIUM_THRESHOLD, config.max_prime);
return {SMALL_THRESHOLD, MEDIUM_THRESHOLD};
}
double combined_sieve_method2_time_estimate(
const struct Config& config,
const mpz_t &K,
uint64_t valid_ms,
double prp_time_est) {
// XXX: pull these from config file or somewhere
const double INVERSES_SECS = 18e-9;
const double MODULE_SEARCH_SECS = 125e-9;
// much less important to correctly set.
const double COUNT_VECTOR_BOOL_PER_SEC = 6871000500;
// ~ `primesieve -t1 500e9 --dist 1e9'
const double PRIME_RANGE_SEC = 0.26 / 1e9;
const size_t coprimes = 2 * count_coprime_sieve(config);
const auto THRESHOLDS = calculate_thresholds_method2(config, coprimes, valid_ms);
const size_t s_threshold_primes = primepi_estimate(THRESHOLDS.first);
const size_t m_threshold_primes = primepi_estimate(THRESHOLDS.second);
const size_t expected_primes = primepi_estimate(config.max_prime);
// Time to compute all (primes % K)
const double K_log = _log(K);
const double mod_time_est = benchmark_primorial_modulo(K, 100'000 * (K_log < 2000 ? 20 : 1));
const double k_mod_time = expected_primes * mod_time_est;
// Time for SMALL_THRESHOLD to MEDIUM_THRESHOLD
const size_t inverses = (m_threshold_primes - s_threshold_primes) * coprimes;
const double inverse_time = inverses * INVERSES_SECS;
// Time for solving module_search
const size_t interval = 2 * config.sieve_length + 1;
const size_t expected_m_stops =
(log(log(config.max_prime)) - log(log(THRESHOLDS.second))) * interval * config.minc;
const size_t solves = (expected_m_stops + (expected_primes - m_threshold_primes));
const double m_search_time = solves * MODULE_SEARCH_SECS;
const size_t count_prints = 5 * (log10(config.max_prime) - 4);
const double extra_time =
// PrimePi takes ~0.3s / billion
config.max_prime * PRIME_RANGE_SEC +
// 5 prints per log10 * std::count(all_unknowns)
count_prints * 1.0 * valid_ms * coprimes / COUNT_VECTOR_BOOL_PER_SEC;
const double total_estimate = k_mod_time + m_search_time + inverse_time + extra_time;
// Estimate still needs to account for:
// small primes
// marking off factors (small and large)
if (config.verbose >= 2 && config.show_timing) {
const double N_log = K_log + log(config.mstart);
const double prob_prime = 1 / N_log - 1 / (N_log * N_log);
const double estimated_prp_per_m = 1 / (prob_prime * log(config.max_prime) * exp(GAMMA));
const double test_estimate = 2 * valid_ms * estimated_prp_per_m * prp_time_est;
printf("Estimated misc (PrimePi, count unknown, ...) time: %.0f (%.1f%% total)\n",
extra_time, 100.0 * extra_time / total_estimate);
printf("Estimated K mod/s: %'.0f, estimated time for all mods: %.0f (%.1f%% total)\n",
1 / mod_time_est, k_mod_time, 100.0 * k_mod_time / total_estimate);
printf("Estimated modulo_searches(million): %ld, time: %.0f (%.1f%% total)\n",
(expected_m_stops + expected_primes) / 1'000'000,
m_search_time, 100.0 * m_search_time / total_estimate);
printf("Estimated sieve time: %.0f seconds (%.2f hours) (%.3f%%)\n",
total_estimate, total_estimate / 3600,
100 * total_estimate / (test_estimate + total_estimate));
printf("Estimated test time: %.0f hours (%.1f%%)\n",
test_estimate / 3600,
100 * test_estimate / (test_estimate + total_estimate));
printf("\n");
}
return total_estimate;
}
/**
* Handles approx count of divisors by d
* See "Optimizing Choice Of D" in THEORY.md
* Return: Counts the number of coprimes of (N, i), -sl <= i <= sl
*/
std::tuple<double, uint32_t, double, double> count_K_d(const struct Config& config) {
uint64_t K_mod_d;
double N_log;
const uint64_t d = config.d;
{
double K_log;
mpz_t K;
K_stats(config, K, nullptr, &K_log);
// Looks a little silly (P# / d) % d
K_mod_d = mpz_fdiv_ui(K, d);
mpz_clear(K);
N_log = K_log + log(config.mstart);
}
vector<uint32_t> P_primes = get_sieve_primes(config.p);
assert( P_primes.back() == config.p );
// Prob prime if no factor of number less than P
double prob_prime_adj = prob_prime_coprime(config);
if (config.verbose >= 3) {
printf("prob_prime: %.6f => %.6f\n",
1 / N_log - 1 / (N_log * N_log),
prob_prime_adj);
}
// Find factors of D
vector<uint32_t> D_primes;
for (uint32_t prime : P_primes) {
if (d % prime == 0)
D_primes.push_back(prime);
}
const size_t sl = config.sieve_length;
const size_t length = 2 * sl + 1;
// composite from coprime K
char compositeK[length];
std::fill(compositeK, compositeK + length, false);
for (uint32_t prime : P_primes) {
if (d % prime != 0) {
// mark off all multiples of prime
uint32_t first = (sl % prime);
for (size_t m = first; m < length; m += prime) {
compositeK[m] = true;
}
}
}
double expected_length = 0;
size_t expected_count = 0;
double remaining_prob = 0;
char composite[length];
uint64_t m = config.mstart;
// Periodic in d, but d might be X00'000'000 so limit to 5'000
const uint64_t intervals = std::min(d, 5'000UL);
size_t m_count = 0;
for (; m_count < intervals; m++) {
if (m >= config.mstart + config.minc) break; // Tested all values.
if (d > 1 && gcd(m, d) > 1) continue;
m_count++;
// Reset to composites from coprime K
std::copy(compositeK, compositeK + length, composite);
// Handle d primes for this m
for (uint32_t p : D_primes) {
// -((m * K) - SL) % p => (m * K_mod_d + p - (sl % p)) % p
assert(K_mod_d % p != 0);
uint64_t first = (p - (((m % p) * (K_mod_d % p) + p - (sl % p)) % p)) % p;
for (size_t mi = first; mi < length; mi += p) {
composite[mi] = true;
}
}
if (config.verbose >= 3 && m <= 6) {
size_t count_unknown = std::count(composite + sl, composite + 2*sl, false);
printf("%ld * %d#/%ld | %ld | ", m, config.p, d, count_unknown);
for (int x = 0; (size_t) x <= sl; x++)
if (!composite[sl + x])
printf("%d ", x);
printf("\n");
}
for (int dir = -1; dir <= 1; dir += 2) {
double expected = 0;
double prob = 1.0;
for (int x = 0; (size_t) x <= sl; x++) {
if (!composite[sl + dir * x]) {
expected += x * prob * prob_prime_adj;
prob *= 1 - prob_prime_adj;
expected_count += 1;
}
}
expected += sl * prob;
expected_length += expected;
remaining_prob += prob;
}
}
return {expected_length / m_count,
expected_count / (m_count * 2),
remaining_prob / (m_count * 2),
prob_prime_adj
};
}
double prob_prime_and_stats(const struct Config& config, mpz_t &K) {
int K_digits;
double K_log;
K_stats(config, K, &K_digits, &K_log);
if (config.verbose >= 2) {
// From Mertens' 3rd theorem
double unknowns_after_sieve = 1 / (log(config.max_prime) * exp(GAMMA));
const double N_log = K_log + log(config.mstart);
const double prob_prime = 1 / N_log - 1 / (N_log * N_log);
double prob_prime_after_sieve = prob_prime / unknowns_after_sieve;
auto stats = count_K_d(config);
size_t count_coprime_p = std::get<1>(stats);
double prob_prime_coprime_p = std::get<3>(stats);
double prob_gap_hypothetical = std::get<2>(stats);
float expected = count_coprime_p * (prob_prime_coprime_p / prob_prime_after_sieve);
printf("\n");
printf("\texpect %.0f left = 2 * %.0f (%.3f%%) of %u after %ldM\n",
2 * expected, expected, 100.0 * expected / (config.sieve_length + 1),
config.sieve_length, config.max_prime/1'000'000);
printf("\t%.3f%% of %d digit numbers are prime\n",
100 * prob_prime, K_digits);
printf("\t%.3f%% of tests should be prime (%.1fx speedup)\n",
100 * prob_prime_after_sieve, 1 / unknowns_after_sieve);
printf("\t~2x%.1f = %.1f PRP tests per m\n",
1 / prob_prime_after_sieve, 2 / prob_prime_after_sieve);
printf("\tsieve_length=%d is insufficient ~~%.3f%% of time\n",
config.sieve_length, 100 * prob_gap_hypothetical);
printf("\n");
}
return K_log;
}
/**
* Change that a number near K is prime
* GIVEN no factor of K or D => no factor of P#
*/
double prob_prime_coprime(const struct Config& config) {
double N_log = calc_log_K(config) + log(config.mstart);
double prob_prime_coprime_P = 1 / N_log - 1 / (N_log * N_log);
// Adjust for prob_prime for no primes <= P
for (auto prime : get_sieve_primes(config.p)) {
prob_prime_coprime_P /= (1 - 1.0 / prime);
}
return prob_prime_coprime_P;
}
// Small sieve of Eratosthenes.
vector<uint32_t> get_sieve_primes(uint32_t n) {
assert(n < 1'001'000); // Use libprimesieve for larger intervals
vector<uint32_t> primes = {2};
uint32_t half_n = n >> 1;
vector<bool> is_prime(half_n + 1, true);
for (uint32_t p = 3; p <= n; p += 2) {
if (is_prime[p >> 1]) {
primes.push_back(p);
uint64_t p2 = p * p;
if (p2 > n) break;
for (uint32_t m = p2 >> 1; m <= half_n; m += p)
is_prime[m] = false;
}
}
for (uint32_t p = primes.back() + 2; p <= n; p += 2) {
if (is_prime[p >> 1])
primes.push_back(p);
}
return primes;
}
bool is_prime_brute(uint32_t n) {
if ((n & 1) == 0)
return false;
for (uint32_t p = 3; p * p <= n; p += 2)
if (n % p == 0)
return false;
return true;
}
size_t primepi_estimate(uint64_t max_prime) {
// Lookup primepi for common max_prime values.
if (common_primepi.count(max_prime)) {
return common_primepi.at(max_prime);
}
return 1.04 * max_prime / log(max_prime);
}
void Args::show_usage(char* name, Pr program) {
Config defaults;
cout << "Usage: " << name << endl;
cout << "[REQUIRED]" << endl;
cout << " -p <p>" << endl;
cout << " -d <p>" << endl;
cout << " --mstart <start>" << endl;
cout << " --minc <int>" << endl;
cout << "OR" << endl;
cout << " -u, --unknown-filename <filename>" << endl;
cout << " parse p, d, mstart, minc, sieve-length, max-prime from filename" << endl;
cout << endl;
cout << "[OPTIONALLY]" << endl;
if (program == Pr::SIEVE || program == Pr::STATS) {
cout << " -t, --threads N" << endl;
cout << " Use N threads (OpenMP)" << endl;
}
cout << " --min-merit <min_merit>" << endl;
cout << " only display prime gaps with merit >= min_merit" << endl;
if (program == Pr::TEST_GPU) {
cout << " --mskip <start at this m>" << endl;
cout << " allows for partial resume of a unknown-file" << endl;
}
if (program == Pr::SIEVE) {
cout << " --sieve-length" << endl;
cout << " how large the positive/negative sieve arrays should be" << endl;
cout << " --max-prime" << endl;
cout << " use primes <= max-prime (in millions) for checking composite" << endl;
cout << endl;
cout << " --save-unknowns" << endl;
cout << " save unknowns to a temp file where they are processed in a 2nd pass." << endl;
cout << " --rle" << endl;
cout << " save in run-length encoded format" << endl;
cout << " --bitcompress" << endl;
cout << " save in new bitcompressed format" << endl;
cout << " --maxmem <max memory in GB>" << endl;
cout << " Combined sieve will print a warning if it's likely to use more memory." << endl;
}
cout << endl;
cout << "[OPTIONAL]" << endl;
if (program == Pr::SIEVE || program == Pr::STATS) {
cout << " --search-db" << endl;
cout << " Database for this project (Default: '" << defaults.search_db << "')" << endl;
cout << " --prime-gaps-db" << endl;
cout << " Prime gap prime gap search db (Default: '" << defaults.gaps_db << "')" << endl;
}
cout << endl;
cout << " -q, --quiet" << endl;
cout << " suppress some status output (twice for more suppression)" << endl;
cout << " -h, --help" << endl;
cout << " print this help message" << endl;
cout << endl;
cout << "calculates prime_gaps for (mstart + mi) * p#/d, mi <= minc " << endl;
}
std::string Args::gen_unknown_fn(const struct Config& config, std::string suffix) {
if (!config.unknown_filename.empty()) {
// dirname (unknown/ or input) handled by parse.
// re-generating unknown_fn can cause issue (with losing dirname)
return config.unknown_filename;
}
return "unknowns/" +
std::to_string(config.p) + "_" +
std::to_string(config.d) + "_" +
std::to_string(config.mstart) + "_" +
std::to_string(config.minc) + "_s" +
std::to_string(config.sieve_length) + "_l" +
std::to_string(config.max_prime / 1'000'000) + "M" +
(config.method1 ? ".m1" : "") +
suffix;
}
int Args::guess_compression(const struct Config& config, std::ifstream& unknown_file) {
// Get current position
int pos = unknown_file.tellg();
assert(pos == 0);
// 100 characters gets past <m>: -count count | <OFFSETS>
// Check that <m> is <m> not <mi>
{
int64_t mtest = -1;
unknown_file >> mtest;
assert(mtest >= 0);
int64_t m = config.mstart;
for (; gcd(m, config.d) > 1; m++);
if (m != mtest) {
cout << endl;
cout << "file format has changed," << endl;
cout << "lines should start with <mstart + mi> not <mi>" << endl;
cout << "\texpected: " << m << " found: " << mtest << endl;
cout << "you can add <mstart> to each line, recreate, or git checkout 74241f7c" << endl;
cout << "Sorry" << endl;
exit(1);
}
}
char t[100] = {0};
unknown_file.read(t, sizeof(t) - 1);
unknown_file.seekg(pos, std::ios_base::beg);
// Compression 2 uses || seperator
for (size_t i = 0; i < strlen(t) - 1; i++) {
if (t[i] == '|') {
assert(i + 1 < strlen(t));
if (t[i + 1] == '|')
return 2;
break;
}
}
bool has_space = false;
bool has_high_range = false;
for (size_t i = 50; i < strlen(t) - 1; i++) {
has_space |= t[i] == ' ' && t[i+1] != '|' && t[i-1] != '|';
has_high_range |= t[i] > '9';
}
assert(has_space ^ has_high_range);
return has_high_range ? 1 : 0;
}
Config Args::argparse(int argc, char* argv[], Pr program) {
// NOTE: Remember to add to getopt_long(argc, argv, OPTIONS_STRING, ...) below
static struct option long_options[] = {
{"p", required_argument, 0, 'p' },
{"d", required_argument, 0, 'd' },
{"mstart", required_argument, 0, 1 },
{"minc", required_argument, 0, 2 },
{"mskip", required_argument, 0, 16 },
{"unknown-filename", required_argument, 0, 'u' },
{"sieve-length", required_argument, 0, 4 },
{"max-prime", required_argument, 0, 5 },
{"threads", required_argument, 0, 't' },
{"min-merit", required_argument, 0, 3 },
{"save", no_argument, 0, 7 },
{"save-unknowns", no_argument, 0, 7 },
{"rle", no_argument, 0, 13 },
{"bitcompressed", no_argument, 0, 15 },
{"uncompressed", no_argument, 0, 17 },
{"max-mem", required_argument, 0, 14 },
{"search-db", required_argument, 0, 9 },
{"prime-gaps-db", required_argument, 0, 10 },
{"method1", no_argument, 0, 8 },
// Secret option
{"hide-timing", no_argument, 0, 11 },
{"testing", no_argument, 0, 12 },
{"quiet", no_argument, 0, 'q' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
Config config;
config.valid = 1;
int option_index = 0;
char c;
while ((c = getopt_long(argc, argv, "qhp:d:u:t:", long_options, &option_index)) >= 0) {
switch (c) {
case 'h':
show_usage(argv[0], program);
exit(0);
case 'q':
config.verbose--;
break;
case 'p':
config.p = atoi(optarg);
break;
case 'd':
config.d = atoi(optarg);
break;
case 1:
config.mstart = atoll(optarg);
break;
case 2:
config.minc = atoll(optarg);
break;
case 16:
config.mskip = atoll(optarg);
break;
case 'u':
{
// Ugh, change to c++17 filesystem::path at some later point
char* t = strdup(optarg);
string dir = dirname(t);
free(t);
char* copy = strdup(optarg);
t = basename(optarg);
assert(*t != 0);
assert(strcmp(t, ".") != 0);
// Add "unknowns/" if no directory present
dir = (dir == ".") ? UNKNOWNS_DIR : dir;
config.unknown_filename = dir + "/" + t;
assert( std::count(t, t + strlen(t), '_') == 5);
config.p = atoi(t);
t = std::strchr(t, '_');
t++;
config.d = atoi(t);
t = std::strchr(t, '_');
t++;
config.mstart = atoll(t);
t = std::strchr(t, '_');
t++;
config.minc = atoll(t);
t = std::strchr(t, '_');
assert( t[0] == '_' && t[1] == 's' );
t += 2;
config.sieve_length = atoi(t);
t = std::strchr(t, '_');
assert( t[0] == '_' && t[1] == 'l' );
t += 2;
config.max_prime = atol(t) * 1'000'000;
t = std::strchr(t, 'M');
config.method1 = (t[3] == '1');
assert( std::strcmp(t, "M.txt") == 0 || std::strcmp(t, "M.m1.txt") == 0 );
free(copy);
}
break;
case 't':
config.threads = atoi(optarg);
break;
case 3:
config.min_merit = atof(optarg);
break;
case 4:
config.sieve_length = atoi(optarg);
break;
case 5:
config.max_prime = atol(optarg) * 1'000'000;
break;
case 7:
config.save_unknowns = true;
break;
case 8:
config.method1 = true;
break;
case 9:
config.search_db = optarg;
assert_file_exists(optarg);
break;
case 10:
config.gaps_db = optarg;
assert_file_exists(optarg);
break;
case 11:
config.show_timing = false;
break;
case 12:
config.testing = true;
break;
case 13:
config.compression = 1;
break;
case 15:
config.compression = 2;
break;
case 17:
config.compression = 3;
break;
case 14:
config.max_mem = atol(optarg);
break;
case 0:
printf("option %s arg %s\n", long_options[option_index].name, optarg);
config.valid = 0;
break;
case '?':
config.valid = 0;
break;
default:
config.valid = 0;
printf("getopt returned \"%d\"\n", c);
}
}
if (optind < argc) {
config.valid = 0;
printf("unknown positional argument: ");
while (optind < argc) {
printf("%s ", argv[optind++]);
}
printf("\n");
}