// g2fold.c -- G2(x#) by folding the twin-slot cycle one prime at a time. solveathome job #7; // shares no code with the primeoire repo and none with g2sieve.c. // // A tile is (s0, P, D, gap cycle): its slots s0 < s1 < ... < s_{D-1} in [0,P) are the r with // r, r+2 prime to P; gap[i] = s_{i+1} - s_i and gap[D-1] = s0 + P - s_{D-1} (the wrap). // Folding by the next prime q walks the lifts s_i + kP, k = 0..q-1, in increasing order and drops // those that are 0 or -2 mod q. From x = 3 every gap is a multiple of 6; one byte holds gap/6. // Every built tile is checked: slot count = D*(q-2) and gaps sum to qP. // // ./g2fold T [stream levels...] builds T_3 .. T_29 in memory and prints G2, least, nmax, D; // prints maxsum_m(T_19), maxsum_m(T_23) for m = 1..24 and the // THRESH table; then for each stream level (31 or 37) walks // the lifts of T_29 without storing them, with T threads. // stdout is deterministic (hash it); timings go to stderr. #include #include #include #include #include #include typedef uint64_t u64; typedef unsigned long long ull; typedef struct { int x; u64 s0, P, D; uint8_t *g; } Tile; static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + 1e-9 * t.tv_nsec; } static void die(const char *m) { fprintf(stderr, "FATAL: %s\n", m); exit(1); } static Tile fold(const Tile *t, int q) { Tile n; n.x = q; n.P = t->P * q; n.D = t->D * (q - 2); n.g = malloc(n.D); if (!n.g) die("malloc"); u64 pos = t->s0, rq = t->s0 % q, cnt = 0, first = 0, last = 0; for (int k = 0; k < q; k++) for (u64 i = 0; i < t->D; i++) { if (rq != 0 && rq != (u64)(q - 2)) { if (cnt == 0) first = pos; else { u64 gap = pos - last; if (gap % 6 || gap / 6 > 255 || cnt > n.D) die("gap"); n.g[cnt - 1] = (uint8_t)(gap / 6); } last = pos; cnt++; } u64 step = 6 * (u64)t->g[i]; pos += step; rq = (rq + step) % q; } if (cnt != n.D) die("slot count != D*(q-2)"); u64 wrap = first + n.P - last; if (wrap % 6 || wrap / 6 > 255) die("wrap gap"); n.g[cnt - 1] = (uint8_t)(wrap / 6); n.s0 = first; u64 sum = 0; for (u64 i = 0; i < n.D; i++) sum += 6 * (u64)n.g[i]; if (sum != n.P) die("gaps do not sum to P"); return n; } static void tile_report(const Tile *t) { u64 pos = t->s0, best = 0, least = 0, nm = 0; for (u64 i = 0; i < t->D; i++) { u64 gap = 6 * (u64)t->g[i]; if (gap > best) { best = gap; least = pos; nm = 1; } else if (gap == best) nm++; pos += gap; } printf("x=%-3d P=%-16llu D=%-14llu G2=%-5llu least=%-18llu nmax=%-3llu [fold]\n", t->x, (ull)t->P, (ull)t->D, (ull)best, (ull)least, (ull)nm); fflush(stdout); } #define MMAX 26 static void maxsums(const Tile *t, u64 *out) { // out[m] = max over i of gap[i] + ... + gap[i+m-1], cyclic for (int m = 1; m <= MMAX; m++) { u64 s = 0, best = 0; for (int j = 0; j < m; j++) s += t->g[j % t->D]; for (u64 i = 0; i < t->D; i++) { if (s > best) best = s; s += t->g[(i + m) % t->D]; s -= t->g[i]; } out[m] = 6 * best; } } // ---------------------------------------------------------------- streaming fold typedef struct { const Tile *t; u64 q0, q1; u64 ka, kb; u64 first, last, max, least, nmax, count; } SJob; static void *stream_worker(void *arg) { SJob *J = arg; const Tile *t = J->t; u64 q0 = J->q0, q1 = J->q1; // q1 = 1 means a single fold prime uint8_t d0[64] = {0}, d1[64] = {0}, s0t[256], s1t[256]; d0[0] = 1; d0[q0 - 2] = 1; if (q1 > 1) { d1[0] = 1; d1[q1 - 2] = 1; } for (int c = 0; c < 256; c++) { s0t[c] = (6 * c) % q0; s1t[c] = (6 * c) % q1; } u64 pos = t->s0 + J->ka * t->P, r0 = pos % q0, r1 = pos % q1; u64 first = UINT64_MAX, last = 0, mx = 0, least = UINT64_MAX, nm = 0, cnt = 0; const uint8_t *g = t->g; const u64 D = t->D; for (u64 k = J->ka; k < J->kb; k++) for (u64 i = 0; i < D; i++) { if (!(d0[r0] | d1[r1])) { if (cnt) { u64 gap = pos - last; if (gap > mx) { mx = gap; least = last; nm = 1; } else if (gap == mx) nm++; } else first = pos; last = pos; cnt++; } uint8_t c = g[i]; pos += 6 * (u64)c; r0 += s0t[c]; if (r0 >= q0) r0 -= q0; r1 += s1t[c]; if (r1 >= q1) r1 -= q1; } J->first = first; J->last = last; J->max = mx; J->least = least; J->nmax = nm; J->count = cnt; return NULL; } static void stream(const Tile *t, int level, int T) { u64 q0 = 0, q1 = 0, D = 0; if (t->x != 29) die("stream expects T_29"); if (level == 31) { q0 = 31; q1 = 1; D = t->D * 29; } else if (level == 37) { q0 = 31; q1 = 37; D = t->D * 29 * 35; } else die("stream level must be 31 or 37"); u64 M = q0 * q1, P = t->P * M; double t0 = now(); SJob *J = calloc(T, sizeof(SJob)); pthread_t *th = calloc(T, sizeof(pthread_t)); for (int i = 0; i < T; i++) { J[i].t = t; J[i].q0 = q0; J[i].q1 = q1; J[i].ka = M / T * i; J[i].kb = (i == T - 1) ? M : M / T * (i + 1); pthread_create(&th[i], NULL, stream_worker, &J[i]); } for (int i = 0; i < T; i++) pthread_join(th[i], NULL); u64 F = UINT64_MAX, L = 0, MX = 0, LE = UINT64_MAX, NM = 0, C = 0; for (int i = 0; i < T; i++) { SJob *c = &J[i]; if (c->count == 0) continue; if (C == 0) F = c->first; else { u64 gap = c->first - L; if (gap > MX) { MX = gap; LE = L; NM = 1; } else if (gap == MX) { NM++; if (L < LE) LE = L; } } if (c->nmax) { if (c->max > MX) { MX = c->max; LE = c->least; NM = c->nmax; } else if (c->max == MX) { NM += c->nmax; if (c->least < LE) LE = c->least; } } L = c->last; C += c->count; } u64 wrap = F + P - L; if (wrap > MX) { MX = wrap; LE = L; NM = 1; } else if (wrap == MX) { NM++; if (L < LE) LE = L; } printf("x=%-3d P=%-16llu D=%-14llu G2=%-5llu least=%-18llu nmax=%-3llu D==prod(p-2):%s [fold-stream]\n", level, (ull)P, (ull)C, (ull)MX, (ull)LE, (ull)NM, C == D ? "yes" : "NO"); fflush(stdout); fprintf(stderr, "stream x=%d T=%d %.1f s\n", level, T, now() - t0); free(J); free(th); } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: g2fold T [31] [37]\n"); return 2; } int T = atoi(argv[1]); double t0 = now(); // T_3: the only r in [0,6) with r, r+2 prime to 6 is 5 Tile t = { 3, 5, 6, 1, malloc(1) }; t.g[0] = 1; tile_report(&t); static const int Q[] = {5, 7, 11, 13, 17, 19, 23, 29}; Tile t19 = {0}, t23 = {0}; for (int i = 0; i < 8; i++) { Tile n = fold(&t, Q[i]); if (t.x != 3 && t.x != 19 && t.x != 23) free(t.g); // T_19 and T_23 are kept for maxsum t = n; tile_report(&t); if (t.x == 19) t19 = t; if (t.x == 23) t23 = t; } fprintf(stderr, "tiles to T_29 built in %.1f s\n", now() - t0); u64 m19[MMAX + 1], m23[MMAX + 1]; maxsums(&t19, m19); maxsums(&t23, m23); printf("\nmaxsum_m(T_v): largest sum of m consecutive gaps of the cycle\n m T_19 T_23\n"); for (int m = 1; m <= MMAX; m++) printf("%3d %6llu %6llu\n", m, (ull)m19[m], (ull)m23[m]); // the nine (v, b, THRESH, G2) rows of research/exact-g2-ladder.js, section 2 static const int RUNS[9][4] = {{19,31,6,348},{23,31,3,348},{19,37,11,528},{19,41,12,546},{23,41,9,546}, {19,41,11,546},{23,41,7,546},{19,43,12,618},{23,43,9,618}}; printf("\nfilter bound: runs of L dead T_v slots span L+1 gaps. If runs with L >= THRESH are inspected,\n" "a missed gap is <= maxsum_THRESH; if only L > THRESH, it is <= maxsum_(THRESH+1).\n"); printf(" v b THRESH G2 maxsum_T sound(L>=T) maxsum_(T+1) sound(L>T) largest safe T for L>=T\n"); for (int i = 0; i < 9; i++) { int v = RUNS[i][0], b = RUNS[i][1], T0 = RUNS[i][2], g = RUNS[i][3]; u64 *ms = v == 19 ? m19 : m23; int safe = 0; for (int m = 1; m <= MMAX; m++) if (ms[m] < (u64)g) safe = m; printf("%3d %3d %5d %6d %8llu %-5s %10llu %-5s %d\n", v, b, T0, g, (ull)ms[T0], ms[T0] < (u64)g ? "yes" : "NO", (ull)ms[T0 + 1], ms[T0 + 1] < (u64)g ? "yes" : "NO", safe); } fflush(stdout); free(t23.g); free(t19.g); for (int i = 2; i < argc; i++) stream(&t, atoi(argv[i]), T); return 0; }