#!/usr/bin/env node // Job 2821 (triage of route 133): exact K*(s) at the small levels, so the ladder the route proposes // is decided where it is CHEAP rather than where it is expensive. // // Definition (route 133 / #609 / review #104, used verbatim): // level s: P = prod of primes <= s // a level-s slot is an integer r with gcd(r,P) = gcd(r+2,P) = 1 // Q(s) = { primes q : s < q <= 2s } // r is killed by q iff q | r or q | r+2 // K*(s) = the longest run of CONSECUTIVE SLOTS (adjacent elements of the slot list, // not consecutive integers -- two consecutive integers cannot both be slots // once 2 | P) each killed by some q in Q(s). // // The (slot, kill) pattern is periodic with period N = P * prod(Q), so enumerating one period is // exact, not a lower bound. Every slot r in [0,N) has a unique form slots[i] + t*P, and the order // of slots is lexicographic in (t, i); the k-th slot of the next period follows the last slot of // this one, so one circular pass over the flattened sequence gives the maximum run. // // Cost is |slots| * prod(Q) * |Q| basic lookups: 1.3e7 at s=12, 1.1e7 at s=13, 3.2e8 at s=15. // s=16 needs 9.9e9 and is refused (that is the wall the route records, placed precisely). // // node kstar-ladder.mjs --control reproduce #609 C1 (s = 5,6,7,9,10,11) // node kstar-ladder.mjs --levels 5-15 exact K* for a range // node kstar-ladder.mjs --levels 13 --budget 1e9 // // Output: JSON to --out, including a WITNESS (the run's slots and their killing primes) so the // value can be re-checked by independent integer arithmetic, and timing per level. import { writeFileSync } from "node:fs"; function gcd(a, b) { while (b) { const t = a % b; a = b; b = t; } return a; } function primesUpTo(n) { const out = []; for (let m = 2; m <= n; m++) { let p = true; for (const d of out) { if (d * d > m) break; if (m % d === 0) { p = false; break; } } if (p) out.push(m); } return out; } const prod = (a) => a.reduce((x, y) => x * y, 1); function slotsModP(P) { const s = []; for (let r = 0; r < P; r++) if (gcd(r, P) === 1 && gcd(r + 2, P) === 1) s.push(r); return s; } // exact K*(s), returning {K, witness} function kstar(s, { maxWork = 4e8 } = {}) { const P = prod(primesUpTo(s)); const Q = primesUpTo(2 * s).filter((q) => q > s); const slots = slotsModP(P); const S = slots.length, T = prod(Q); const work = S * T; if (work > maxWork) return { s, P, Q, S, T, work, refused: true }; // kill[q] : Uint8Array(S*q), entry (i, u) = 1 iff slot(slots[i] + t*P) is killed by q when t = u mod q const kill = Q.map((q) => { const tab = new Uint8Array(S * q); const step = P % q; for (let i = 0; i < S; i++) { const a = slots[i] % q; for (let u = 0; u < q; u++) { const r = (a + u * step) % q; tab[i * q + u] = (r === 0 || r === q - 2) ? 1 : 0; } } return { q, tab }; }); const K = kill.length; const qs = kill.map((k) => k.q), tabs = kill.map((k) => k.tab); let maxRun = 0, run = 0, bestEnd = -1; const rem = new Int32Array(K); // t mod q per prime, hoisted for (let t = 0; t < T; t++) { for (let k = 0; k < K; k++) rem[k] = t % qs[k]; for (let i = 0; i < S; i++) { let hit = 0; for (let k = 0; k < K; k++) { if (tabs[k][i * qs[k] + rem[k]]) { hit = 1; break; } } if (hit) { run++; if (run > maxRun) { maxRun = run; bestEnd = t * S + i; } } else { run = 0; } } } // The pattern is circular: a maximal run may straddle the period seam, so the trailing ones and // the leading ones are added. let prefix = 0; for (let i = 0; i < S; i++) { let hit = 0; for (let k = 0; k < K; k++) { if (tabs[k][i * qs[k] + (0 % qs[k])]) { hit = 1; break; } } if (hit) prefix++; else break; } let suffix = 0; const tl = (T - 1) % qs.map ? T - 1 : T - 1; outer: for (let t = tl; t >= 0; t--) { for (let i = S - 1; i >= 0; i--) { let hit = 0; for (let k = 0; k < K; k++) { if (tabs[k][i * qs[k] + (t % qs[k])]) { hit = 1; break; } } if (hit) suffix++; else break outer; } } const circular = Math.max(maxRun, suffix + prefix); // witness: walk backwards from bestEnd through the flattened sequence const witness = []; if (bestEnd >= 0) { const mod = S * T; // if the circular maximum is the seam sum, the run starts at the very beginning of the period const end = (circular > maxRun) ? (mod - 1) : bestEnd; for (let n = circular; n >= 1; n--) { let f = (end - (n - 1)) % mod; if (f < 0) f += mod; const t = Math.floor(f / S), i = f % S; const r = slots[i] + t * P; const killers = Q.filter((q) => r % q === 0 || (r + 2) % q === 0); witness.push({ r, killers, isSlot: gcd(r, P) === 1 && gcd(r + 2, P) === 1 }); } } return { s, P, Q, S, T, work, K: circular, maxInternal: maxRun, prefix, suffix, witness }; } function parseLevels(spec) { const m = /^(\d+)(?:-(\d+))?$/.exec(spec); if (!m) throw new Error("bad --levels " + spec); const a = +m[1], b = m[2] ? +m[2] : a, out = []; for (let s = a; s <= b; s++) out.push(s); return out; } const C1 = { 5: 2, 6: 3, 7: 3, 9: 5, 10: 8, 11: 6 }; // #609's control table, verbatim const argv = process.argv.slice(2); const arg = (name, dflt) => { const i = argv.indexOf(name); return i < 0 ? dflt : argv[i + 1]; }; const control = argv.includes("--control"); const levels = parseLevels(arg("--levels", control ? "5-11" : "5-15")); const maxWork = Number(arg("--budget", "4e8")); const out = arg("--out", "job2821/kstar-ladder.json"); const rows = []; for (const s of levels) { const t0 = Date.now(); const r = kstar(s, { maxWork }); const secs = (Date.now() - t0) / 1000; if (r.refused) { rows.push({ s, P: r.P, Q: r.Q, S: r.S, T: r.T, work: r.work, refused: true, seconds: secs }); continue; } rows.push({ s, P: r.P, Q: r.Q, S: r.S, T: r.T, work: r.work, K: r.K, maxInternal: r.maxInternal, seconds: secs, witness: r.witness }); const ctl = C1[s] === undefined ? "" : (C1[s] === r.K ? " == C1 OK" : " != C1 " + C1[s] + " MISMATCH"); console.log(["s=" + String(s).padEnd(3), "P=" + String(r.P).padEnd(9), "|Q|=" + r.Q.length, "slots=" + String(r.S).padEnd(6), "lifts=" + String(r.T).padEnd(9), "work=" + r.work.toExponential(2).padEnd(9), "K*=" + String(r.K).padEnd(4), secs.toFixed(1) + "s", ctl].join(" ")); } let payload = { tool: "kstar-ladder/1.0.0", definition: "route133/#609/review104 verbatim; full-period enumeration", rows }; let refusals = rows.filter((r) => r.refused); if (control) { const bad = levels.filter((s) => rows.find((r) => r.s === s && C1[s] !== undefined && r.K !== C1[s])); payload.control = { table: C1, levels, mismatches: bad, ok: bad.length === 0 }; console.log(bad.length ? "\nCONTROL FAILED at s=" + bad.join(",") : "\nCONTROL: every C1 level reproduced exactly"); } // the ladder and the boundary transfer, from the values actually computed here const byS = Object.fromEntries(rows.filter((r) => !r.refused).map((r) => [r.s, r.K])); const primes = primesUpTo(50); const stats = []; for (const p of primes) { const nx = primes[primes.indexOf(p) + 1]; if (!nx || !(p in byS) || !(nx in byS)) continue; const pre = byS[nx - 1]; stats.push({ block: p, p_k: p, p_next: nx, K_at_p_k: byS[p], K_pre_boundary: pre, K_at_p_next: byS[nx], D_k_first_rung_to_first_rung: byS[nx] - byS[p], delta_boundary: byS[nx] - pre, interior_gain: pre - byS[p], E_k: primesUpTo(2 * nx - 2).length - primesUpTo(2 * p).length }); } payload.block_stats = stats; if (stats.length) { console.log("\nblock | K*(p_k) | K*(p_next-1) | K*(p_next) | D_k (=first rung to first rung) | delta (pre->post boundary) | interior gain | E_k"); for (const b of stats) console.log("%5d | %7d | %12d | %10d | %32d | %27d | %13d | %3d", b.block, b.K_at_p_k, b.K_pre_boundary, b.K_at_p_next, b.D_k_first_rung_to_first_rung, b.delta_boundary, b.interior_gain, b.E_k); } if (refusals.length) console.log("\nREFUSED (work over budget): " + refusals.map((r) => "s=" + r.s + " (" + r.work.toExponential(2) + ")").join(", ")); writeFileSync(out, JSON.stringify(payload, null, 1) + "\n"); console.log("\nwrote " + out);