/* jtwin.c -- exact twin-Jacobsthal ladder G_2(p_n#) by a complete two-class * covering search, with the reduction proved in ../DERIVATION.md. * * ============================== THE REDUCTION ============================== * Fix n and let P' = prod of the primes 5 <= p <= p_n. Put s_p = 6^{-1} mod p * and c_p = 2 s_p mod p. Call k a CANDIDATE when gcd(6k-1,P') = gcd(6k+1,P') * = 1. Then * (i) every integer m with 6 !| m is killed, because 2 | m-1 or m+1 when m * is odd and 3 | m-1 or m+1 when 3 !| m; * (ii) hence the maximal run of consecutive killed positions is * 6 * (max gap between consecutive candidates) - 1 , * and that gap is one more than the maximal run of consecutive * NON-candidate k; * (iii) k is NOT a candidate <=> k = +-s_p (mod p) for some 5 <= p <= p_n * <=> k lies in a pair {a_p, a_p + c_p} for a residue a_p mod p. * Because the k-window may be translated, the residues a_p are free; CRT shows * that every independent choice of the a_p is realised simultaneously by the * single shift A = -(s_p + a_p) (mod p). So, with R(n) the maximal length of * an interval of k covered by one pair {a_p, a_p + c_p} per prime, * a(m) = A144311(m) = 6 R(n) + 5 , G_2(p_n#) = 6 R(n) + 6 , * where m = n + 2 is the A144311 index (its primes include 2 and 3). * * DECISION PROCEDURE. Coverability of [0,R-1] is monotone in R, so R(n) is * found by binary search on R. For fixed R the search assigns the primes * 5,7,...,p_n in increasing order, one residue each, trying every residue; * with U the number of uncovered positions and v_p(r) the number of uncovered * positions in the pair {r, r-c_p} (mod p) of a still-free prime p, * S := sum over free p of max_r v_p(r) * bounds the coverage still available, so a node dies when S < U. The search * is complete: refuting R proves a(m) < 6R + 5. Every claimed covering is * re-verified directly before it is accepted. * * Build: cc -O3 -o jtwin jtwin.c * Use: ./jtwin n Rlo Rhi [seed] [threads] * ./jtwin -brute P (independent period brute force) * ./jtwin -greedy n [R] [reps] [seed] */ #include #include #include #include #include #define MAXNP 64 #define MAXR 8192 #define MAXP 512 /* A144311, published terms 1..22 (OEIS, accessed 2026-09-21) */ static const int KNOWN_A[23] = {0, 1, 5, 11, 29, 41, 65, 107, 149, 203, 257, 347, 527, 545, 617, 707, 869, 965, 1079, 1283, 1397, 1529, 1709}; static int pl[MAXNP]; static int cl[MAXNP]; static int NP; typedef struct { int R; unsigned char cov[MAXR]; int uncov; int asg[MAXNP]; int v[MAXNP][MAXP]; int hist[8 * MAXR]; int histn; int lo; /* leftmost uncovered position: the blocking one */ long long nodes, fails; int sol[MAXNP]; } Ctx; static Ctx base; static volatile int g_stop; /* set the moment a witness is verified: the verdict is decided */ static void init_primes(int n) { int c = 0, x = 5; while (c < n) { int isp = 1, d; for (d = 2; d * d <= x; d++) if (x % d == 0) { isp = 0; break; } if (isp) pl[c++] = x; x++; } NP = n; for (int i = 0; i < NP; i++) { int p = pl[i], s = 0; while ((6 * s) % p != 1) s++; cl[i] = (2 * s) % p; } } static void reset(Ctx *c, int R) { int i, r, j; c->R = R; c->uncov = R; c->histn = 0; c->lo = 0; c->nodes = c->fails = 0; memset(c->cov, 0, (size_t)R); for (i = 0; i < NP; i++) { c->asg[i] = -1; c->sol[i] = -1; } for (i = 0; i < NP; i++) { int q = pl[i], cc = cl[i]; for (r = 0; r < q; r++) { int cnt = 0; for (j = r; j < R; j += q) cnt++; for (j = (r - cc % q + q) % q; j < R; j += q) cnt++; c->v[i][r] = cnt; } } } static void upd_v(Ctx *c, int j, int delta, int skip) { for (int t = 0; t < NP; t++) { if (t == skip || c->asg[t] >= 0) continue; int q = pl[t], cc = cl[t]; c->v[t][j % q] += delta; c->v[t][(j % q + cc) % q] += delta; } } /* mark the pair {a, a+c_i} of prime i as used; return the history level */ static int assign_rec(Ctx *c, int i, int a) { int p = pl[i], cc = cl[i], r2 = (a + cc) % p, j; int saved = c->histn; c->asg[i] = a; for (j = a; j < c->R; j += p) if (!c->cov[j]) { c->cov[j] = 1; c->uncov--; upd_v(c, j, -1, i); c->hist[c->histn++] = j; } for (j = r2; j < c->R; j += p) if (!c->cov[j]) { c->cov[j] = 1; c->uncov--; upd_v(c, j, -1, i); c->hist[c->histn++] = j; } while (c->lo < c->R && c->cov[c->lo]) c->lo++; return saved; } static void unassign_rec(Ctx *c, int i, int saved) { while (c->histn > saved) { int j = c->hist[--c->histn]; c->cov[j] = 0; c->uncov++; if (j < c->lo) c->lo = j; for (int t = 0; t < NP; t++) { if (t == i || c->asg[t] >= 0) continue; int q = pl[t], cc = cl[t]; c->v[t][j % q]++; c->v[t][(j % q + cc) % q]++; } } c->asg[i] = -1; } static int bound_ok(Ctx *c) { int s = 0; if (c->uncov == 0) return 1; for (int t = 0; t < NP; t++) { if (c->asg[t] >= 0) continue; int mx = 0, q = pl[t]; for (int r = 0; r < q; r++) if (c->v[t][r] > mx) mx = c->v[t][r]; s += mx; if (s >= c->uncov) return 1; } return 0; } static int verify_solution(const int *sol, int R) { for (int j = 0; j < R; j++) { int hit = 0; for (int i = 0; i < NP; i++) { int p = pl[i], cc = cl[i]; if (sol[i] < 0) continue; if (j % p == sol[i] || j % p == (sol[i] + cc) % p) { hit = 1; break; } } if (!hit) return 0; } return 1; } /* How far does a witness actually reach? feasible(R) stops as soon as [0,R-1] is * covered, so the banked bound must be taken from the witness, not from R. */ static int covered_prefix(const int *sol, int cap) { for (int j = 0; j < cap; j++) { int hit = 0; for (int i = 0; i < NP; i++) { if (sol[i] < 0) continue; int p = pl[i], cc = cl[i]; if (j % p == sol[i] || j % p == (sol[i] + cc) % p) { hit = 1; break; } } if (!hit) return j; } return cap; } /* primes assigned in increasing order; k = index of the next free prime */ static int dfs_ord(Ctx *c, int k) { if (g_stop) { c->fails++; return 0; } /* a witness is in hand; this branch cannot matter */ c->nodes++; if (c->uncov == 0) { for (int i = 0; i < NP; i++) c->sol[i] = c->asg[i]; return 1; } if (k >= NP) { c->fails++; return 0; } if (!bound_ok(c)) { c->fails++; return 0; } /* Branch order: the two residues of prime k that COVER THE LEFTMOST * UNCOVERED POSITION first (so the first descent is solution-guided and * tackles the blocking position), then the rest by decreasing coverage of * the still-uncovered positions. Ordering affects speed only: the search * remains complete, so a refutation keeps its force. */ int p = pl[k], cc = cl[k]; int j0 = (c->lo < c->R) ? c->lo : 0; int rA = j0 % p, rB = ((j0 - cc) % p + p) % p; int ord[MAXP], key[MAXP]; for (int r = 0; r < p; r++) { ord[r] = r; key[r] = c->v[k][r] + ((r == rA || r == rB) ? 1000 : 0); } for (int i = 1; i < p; i++) { /* descending by key */ int ki = ord[i], kv = key[i], j = i - 1; while (j >= 0 && key[j] < kv) { ord[j+1] = ord[j]; key[j+1] = key[j]; j--; } ord[j+1] = ki; key[j+1] = kv; } int did_zero = 0; for (int t = 0; t < p; t++) { int r = ord[t]; if (c->v[k][r] == 0) { if (did_zero) continue; did_zero = 1; } int sv = assign_rec(c, k, r); if (dfs_ord(c, k + 1)) return 1; unassign_rec(c, k, sv); } c->fails++; return 0; } /* ---- parallel driver: threads split the residues of the first prime ---- */ static int g_np0, g_bP0[MAXP], g_bP1[MAXP]; static volatile int g_next, g_found, g_sol[MAXNP]; static pthread_mutex_t g_mtx = PTHREAD_MUTEX_INITIALIZER; static long long g_nodes, g_fails; /* ---- heartbeat instrumentation (this file only; jtwin.c is untouched) ---- * A detached monitor thread reports the engine's OWN node counters, so a long refutation search is * observable instead of silent. It reads each worker's Ctx->nodes (a benign race, a progress * figure only) and never touches the search itself. */ static Ctx *g_ctx[64]; static volatile int g_ctx_live[64]; static volatile int g_mon_stop, g_mon_gen; static int g_hb_threads, g_hb_R; static void *hb_monitor(void *arg) { (void)arg; int myR = g_hb_R, myGen = g_mon_gen, wpref = -1; double next = 10.0; struct timespec t0, t1, s = { 1, 0 }; clock_gettime(CLOCK_MONOTONIC, &t0); for (;;) { nanosleep(&s, NULL); if (g_mon_stop || myGen != g_mon_gen) break; clock_gettime(CLOCK_MONOTONIC, &t1); double el = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9; if (el >= next) { long long total = 0; int live = 0; for (int i = 0; i < g_hb_threads && i < 64; i++) if (g_ctx_live[i] && g_ctx[i]) { total += g_ctx[i]->nodes; live++; } fprintf(stderr, " HEARTBEAT R=%d t=%.0fs nodes=%lld rate=%.3g/s live=%d/%d branches=%d/%d found=%d\n", myR, el, total, total / el, live, g_hb_threads, g_next, g_np0, g_found); if (g_found) { if (wpref < 0 && pthread_mutex_trylock(&g_mtx) == 0) { wpref = covered_prefix((const int *)g_sol, MAXR - 1); pthread_mutex_unlock(&g_mtx); } if (wpref >= 0) fprintf(stderr, " WITNESS-STATE R=%d prefix=%d => A144311(%d) >= %d\n", myR, wpref, 23, 6 * wpref + 5); } fflush(stderr); next = el + 60.0; } } return NULL; } static void *worker(void *arg) { int id = (int)(long)arg; Ctx *c = (Ctx *)malloc(sizeof(Ctx)); long long ln = 0, lf = 0; if (id >= 0 && id < 64) { g_ctx[id] = c; g_ctx_live[id] = 1; } for (;;) { pthread_mutex_lock(&g_mtx); if (g_found || g_next >= g_np0) { pthread_mutex_unlock(&g_mtx); break; } int b = g_next++; pthread_mutex_unlock(&g_mtx); memcpy(c, &base, sizeof(Ctx)); assign_rec(c, 0, g_bP0[b]); assign_rec(c, 1, g_bP1[b]); if (dfs_ord(c, 2)) { if (!verify_solution(c->sol, c->R)) fprintf(stderr, "SELF-CHECK FAILED at R=%d branch %d\n", c->R, b); pthread_mutex_lock(&g_mtx); if (!g_found) { g_found = 1; g_stop = 1; memcpy((void *)g_sol, c->sol, sizeof(g_sol)); int pref = covered_prefix(c->sol, MAXR - 1); fprintf(stderr, " WITNESS R=%d branch=%d prefix=%d => A144311(%d) >= %d , G_2(%d#) >= %d\n", c->R, b, pref, NP + 2, 6 * pref + 5, pl[NP - 1], 6 * pref + 6); fprintf(stderr, " WITNESS certificate a_p:"); for (int i = 0; i < NP; i++) fprintf(stderr, " %d/%d", c->sol[i], pl[i]); fprintf(stderr, "\n"); fflush(stderr); } pthread_mutex_unlock(&g_mtx); } ln += c->nodes; lf += c->fails; } pthread_mutex_lock(&g_mtx); g_nodes += ln; g_fails += lf; pthread_mutex_unlock(&g_mtx); if (id >= 0 && id < 64) g_ctx_live[id] = 0; free(c); return NULL; } static int feasible(int R, int *sol_out, long long *nodes_out, long long *fails_out, int nthreads) { reset(&base, R); /* Two-level split: 5 x 7 = 35 top branches, for a balanced 8-thread run. */ g_np0 = 0; for (int r0 = 0; r0 < pl[0]; r0++) for (int r1 = 0; r1 < pl[1]; r1++) { g_bP0[g_np0] = r0; g_bP1[g_np0] = r1; g_np0++; } g_next = 0; g_found = 0; g_stop = 0; g_nodes = g_fails = 0; if (nthreads < 1) nthreads = 1; if (nthreads > g_np0) nthreads = g_np0; g_hb_R = R; g_hb_threads = nthreads; g_mon_stop = 0; g_mon_gen++; fprintf(stderr, " RUNG start R=%d threads=%d branches=%d\n", R, nthreads, g_np0); fflush(stderr); pthread_t th[64], mon; if (pthread_create(&mon, NULL, hb_monitor, NULL) == 0) pthread_detach(mon); for (int i = 0; i < nthreads; i++) pthread_create(&th[i], NULL, worker, (void *)(long)i); for (int i = 0; i < nthreads; i++) pthread_join(th[i], NULL); g_mon_stop = 1; if (sol_out) memcpy(sol_out, (const void *)g_sol, sizeof(g_sol)); if (nodes_out) *nodes_out = g_nodes; if (fails_out) *fails_out = g_fails; return g_found; } /* ---------------- randomized greedy: certified lower bound --------------- */ static unsigned long long rs = 88172645463325252ULL; static unsigned long long xr(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; } static int greedy_extend(int target, int *sol_out, int reps) { int cap = target + 600; if (cap > MAXR) cap = MAXR; int best = 0, bestsol[MAXNP]; Ctx *c = (Ctx *)malloc(sizeof(Ctx)); for (int rep = 0; rep < reps; rep++) { reset(c, cap); int order[MAXNP]; for (int i = 0; i < NP; i++) order[i] = i; for (int i = NP - 1; i > 0; i--) { int k = (int)(xr() % (unsigned)(i + 1)); int t = order[i]; order[i] = order[k]; order[k] = t; } for (int i = 0; i < NP; i++) { int idx = order[i], q = pl[idx], bestr = 0, bv = -1; for (int r = 0; r < q; r++) if (c->v[idx][r] > bv) { bv = c->v[idx][r]; bestr = r; } assign_rec(c, idx, bestr); } int j = 0; while (j < cap && c->cov[j]) j++; if (j > best) { best = j; for (int i = 0; i < NP; i++) bestsol[i] = c->asg[i]; } } free(c); if (sol_out) memcpy(sol_out, bestsol, sizeof(bestsol)); return best; } /* ---------------- independent period brute force (tiny n) ---------------- */ static void brute(int n) { int pr[16] = {5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61}; long long P = 1; int run, best, i; long long k, at = -1; for (i = 0; i < n; i++) P *= pr[i]; printf("brute: primes 5..%d, period P' = %lld\n", pr[n-1], P); run = 0; best = 0; for (k = 0; k < P; k++) { int cand = 1; for (i = 0; i < n; i++) if ((6*k-1) % pr[i] == 0 || (6*k+1) % pr[i] == 0) { cand = 0; break; } if (cand) { if (run > best) { best = run; at = k - run; } run = 0; } else run++; } printf("brute R = %d => A144311(%d) = 6R+5 = %d G2 = %d (run starts at k = %lld)\n", best, n + 2, 6*best+5, 6*best+6, at); } int main(int argc, char **argv) { if (argc >= 3 && strcmp(argv[1], "-brute") == 0) { brute(atoi(argv[2])); return 0; } if (argc >= 3 && strcmp(argv[1], "-greedy") == 0) { int n = atoi(argv[2]); int target = (argc > 3) ? atoi(argv[3]) : 400; int reps = (argc > 4) ? atoi(argv[4]) : 400; int seed = (argc > 5) ? atoi(argv[5]) : 12345; init_primes(n); rs = 88172645463325252ULL ^ (unsigned long long)seed; int sol[MAXNP]; int gl = greedy_extend(target, sol, reps); printf("n = %d (primes 5..%d) greedy lower bound R >= %d => A144311(%d) >= %d ; G2(%d#) >= %d\n", n, pl[NP-1], gl, n+2, 6*gl+5, pl[NP-1], 6*gl+6); printf(" residues:"); for (int i = 0; i < NP; i++) printf(" %d/%d", sol[i], pl[i]); printf("\n verified=%d\n", verify_solution(sol, gl)); return 0; } if (argc < 4) { fprintf(stderr, "use: %s n Rlo Rhi [seed] [threads] | -brute P | -greedy n [R] [reps] [seed]\n", argv[0]); return 2; } int n = atoi(argv[1]), Rlo = atoi(argv[2]), Rhi = atoi(argv[3]); int seed = (argc > 4) ? atoi(argv[4]) : 12345; int threads = (argc > 5) ? atoi(argv[5]) : 8; init_primes(n); rs = 88172645463325252ULL ^ (unsigned long long)seed; printf("n = %d primes 5..%d (%d primes) threads = %d\n", n, pl[NP-1], NP, threads); printf(" A144311 index m = %d ; target A144311(%d) = 6R+5\n", n + 2, n + 2); fflush(stdout); int gsol[MAXNP]; int gl = greedy_extend(Rlo + 1, gsol, 200); printf(" greedy lower bound: R >= %d => A144311(%d) >= %d (verified %d)\n", gl, n + 2, 6*gl+5, verify_solution(gsol, gl)); fflush(stdout); int lo = (gl > Rlo) ? gl : Rlo; /* feasible: greedy witness / monotonicity */ int hi = Rhi; /* Increment R from lo+1. Feasible instances are cheap (a witness is found * fast); the expensive step is the single refutation that pins R(n). After * every COVERABLE verdict the witness's TRUE covered prefix is measured and * lo jumps to it, so one search can bank several rungs at once. */ for (int target = lo + 1; ; ) { int sol[MAXNP]; long long nn = 0, ff = 0; if (target > Rhi) { printf("\n ==> INCOMPLETE: no refutation up to R = %d ; A144311(%d) >= %d\n", Rhi, n + 2, 6*Rhi+5); return 0; } clock_t t0 = clock(); int ok = feasible(target, sol, &nn, &ff, threads); int pref = ok ? covered_prefix(sol, MAXR - 1) : target - 1; printf(" R = %-4d %-9s (a=%-6d G2=%-6d) witness prefix %-4d nodes %-13lld (%.1fs)\n", target, ok ? "COVERABLE" : "REFUTED", 6*target+5, 6*target+6, pref, nn, (double)(clock()-t0)/CLOCKS_PER_SEC); fflush(stdout); if (!ok) break; if (pref > target) printf(" witness reaches R = %d => A144311(%d) >= %d , G_2(%d#) >= %d\n", pref, n+2, 6*pref+5, pl[NP-1], 6*pref+6); printf(" certificate a_p:"); for (int i = 0; i < NP; i++) printf(" %d/%d", sol[i], pl[i]); printf("\n"); fflush(stdout); lo = (pref > target) ? pref : target; target = lo + 1; } printf("\n ==> R = %d ; A144311(%d) = %d ; G_2(%d#) = %d\n", lo, n + 2, 6*lo+5, pl[NP-1], 6*lo+6); return 0; }