// capladder.mjs — the refined transport sum's truncation length at every fold #159 ran. // // The old word of the fold T_x -> T_q is T_x. Two ladder objects of that word: // L_anch longest run of consecutive slots whose residue mod q lies in K = {0, q-2}, // i.e. the translate a = 0 of the 2-set {a, a-2}; // L_free the same maximised over every translate a. // // L_star, the largest L with a non-zero Q_L^alt term, is computed on the enumerable // folds by #159's own loop (src/analyze.c, the histQl/histQa block: reach = 3 at // entry, mi = G(L-1) mod q, break on a non-qualifying interior gap, count the // level-L term iff the new reach is non-zero -- the EXISTENTIAL reading, which is // the one that reproduces #159's published numbers). The two folds whose old word // is out of enumeration reach report L_anch and L_free only; the identity // L_star = L_free is what the enumerable folds establish. // // Paths. flat: the old word is resident as gap/6 bytes (T_x, x <= 29). // stream: the old word is generated copy by copy, so T_31 (31 copies of // T_29, 6.2e9 slots) and T_37 (37 copies of T_31, 2.2e11) are never stored. // A streamed run on a small case is gated against the flat answer. // // Usage: node capladder.mjs // env: JOBS=, MAXRUN= import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads'; import { writeFileSync, readFileSync, readdirSync } from 'node:fs'; import { cpus } from 'node:os'; const MAXRUN = Number(process.env.MAXRUN || 24); const JOBS = Number(process.env.JOBS || Math.max(1, Math.min(20, cpus().length - 1))); // HEAVY = both | 31 | 37 | none. The two large folds are the only slow part, so // they can be run in separate invocations; each writes its own JSON beside the main // output for the record. const HEAVY = process.env.HEAVY || (process.env.SKIP_HEAVY === '1' ? 'none' : 'both'); const D_PUB = { 5: 3, 7: 15, 11: 135, 13: 1485, 17: 22275, 19: 378675, 23: 7952175, 29: 214708725, 31: 6226553025, 37: 217929355875 }; // ---- tiles and the segment merge (shared with the bench and the partial passes) import { PRIMES_UPTO, buildTile, gap6Of, foldGap6 } from './job1493-tiles.mjs'; import { mergeSegments, laddersOf } from './job1493-merge.mjs'; // ---- run automaton over slot residues mod q --------------------------------- // `last` and `n` are doubles, not ints: a single segment of the double-folded word // carries ~5.9e9 slots, past the int32 range, and the suffix test `last[a] === n-1` // must stay exact (it is, well inside 2^53). function newAut(q) { return { q, cur: new Int32Array(q), last: new Float64Array(q).fill(-2), best: new Int32Array(q), n: 0 }; } function autStep(A, r) { const q = A.q, t = A.n; let a = r; A.cur[a] = (A.last[a] === t - 1 ? A.cur[a] : 0) + 1; A.last[a] = t; if (A.cur[a] > A.best[a]) A.best[a] = A.cur[a]; a = (r + 2) % q; A.cur[a] = (A.last[a] === t - 1 ? A.cur[a] : 0) + 1; A.last[a] = t; if (A.cur[a] > A.best[a]) A.best[a] = A.cur[a]; A.n = t + 1; } // ---- the producer's window loop (flat, cyclic) ------------------------------ function windowCycle(cls, i, D, q, maxrun) { let reach = 3, L = 2, maxAlt = 1, maxLoose = 1; for (;;) { const mi = cls[(i + L - 1) % D]; if (!(mi === 0 || mi === 2 || mi === q - 2)) break; let nr; if (mi === 0) nr = reach; else if (mi === 2) nr = (reach & 2) ? 1 : 0; else nr = (reach & 1) ? 2 : 0; if (nr) maxAlt = L; maxLoose = L; reach = nr; L++; if (L > maxrun) break; } return { maxAlt, maxLoose }; } function flatAnalyze(g6, q) { const D = g6.length, qm = new Uint8Array(256); for (let b = 0; b < 256; b++) qm[b] = (6 * b) % q; const cls = new Uint8Array(D); for (let i = 0; i < D; i++) cls[i] = qm[g6[i]]; const A = newAut(q); let r = 5 % q; let prefA = [0, 0], prefV = [0, 0]; for (let i = 0; i < D; i++) { if (i === 0) { prefA = [r, (r + 2) % q]; prefV = [1, 1]; } else for (let k2 = 0; k2 < 2; k2++) if (prefV[k2] === i && (prefA[k2] === r || prefA[k2] === (r + 2) % q)) prefV[k2] = i + 1; autStep(A, r); r += cls[i]; if (r >= q) r -= q; } const pref = [], suf = []; for (let k2 = 0; k2 < 2; k2++) if (prefV[k2] > 0) pref.push([prefA[k2], prefV[k2]]); for (let a = 0; a < q; a++) if (A.last[a] === D - 1 && A.cur[a] > 0) suf.push([a, A.cur[a]]); const seg = { n: D, pref, suf, best: Array.from(A.best) }; const best = mergeSegments([seg], q, true); let maxAlt = 0, maxLoose = 0; for (let i = 0; i < D; i++) { const w = windowCycle(cls, i, D, q, MAXRUN); if (w.maxAlt > maxAlt) maxAlt = w.maxAlt; if (w.maxLoose > maxLoose) maxLoose = w.maxLoose; } let Lfree = best[0], aStar = 0; for (let a = 1; a < q; a++) if (best[a] > Lfree) { Lfree = best[a]; aStar = a; } return { D_old: D, L_anch: best[0], L_free: Lfree, aStar, L_star_alt: maxAlt, L_star_loose: maxLoose, best: Array.from(best) }; } // ---- streamed analysis ------------------------------------------------------ // payload: { sab, Dbase, Wbase, pInner, pOuter, q, mode, cFrom, cTo } // mode 'single': old word = fold(base, pInner); copy c <-> sub-copy j = c // mode 'double': old word = fold(fold(base, pInner), pOuter); copy c <-> the // pInner sub-copies of base shifted by c*Wouter function runCopyJobs(payload) { const { sab, Dbase, Wbase, pInner, pOuter, q, mode, cFrom, cTo } = payload; const g6 = new Uint8Array(sab); const qmB = new Uint8Array(256); for (let b = 0; b < 256; b++) qmB[b] = (6 * b) % pInner; const qmQ = new Uint8Array(256); for (let b = 0; b < 256; b++) qmQ[b] = (6 * b) % q; let qmO = null; if (mode === 'double') { qmO = new Uint8Array(256); for (let b = 0; b < 256; b++) qmO[b] = (6 * b) % pOuter; } const WbaseB = Wbase % pInner, WbaseQ = Wbase % q; const WoutQ = (payload.Wouter || 0) % q, WoutO = (payload.Wouter || 0) % pOuter; const segs = []; for (let c = cFrom; c < cTo; c++) { const A = newAut(q); let n = 0; let prefA = [0, 0], prefV = [0, 0]; const sub = mode === 'single' ? 1 : pInner; for (let s = 0; s < sub; s++) { const j = mode === 'single' ? c : s; const k = mode === 'single' ? 0 : c; let rB = (5 + ((j * WbaseB) % pInner)) % pInner; let rQ = (5 + ((j * WbaseQ) % q) + ((k * WoutQ) % q)) % q; let rO = mode === 'double' ? (5 + ((j * (Wbase % pOuter)) % pOuter) + ((k * WoutO) % pOuter)) % pOuter : 0; for (let i = 0; i < Dbase; i++) { const g = g6[i]; const rrB = rB, rrQ = rQ, rrO = rO; rB += qmB[g]; if (rB >= pInner) rB -= pInner; rQ += qmQ[g]; if (rQ >= q) rQ -= q; if (mode === 'double') { rO += qmO[g]; if (rO >= pOuter) rO -= pOuter; } let dead = (rrB === 0 || rrB === pInner - 2); if (mode === 'double' && !dead) dead = (rrO === 0 || rrO === pOuter - 2); if (dead) continue; if (n === 0) { prefA = [rrQ, (rrQ + 2) % q]; prefV = [1, 1]; } else for (let k2 = 0; k2 < 2; k2++) if (prefV[k2] === n && (prefA[k2] === rrQ || prefA[k2] === (rrQ + 2) % q)) prefV[k2] = n + 1; autStep(A, rrQ); n++; } } const pref = [], suf = []; for (let k2 = 0; k2 < 2; k2++) if (prefV[k2] > 0) pref.push([prefA[k2], prefV[k2]]); for (let a = 0; a < q; a++) if (A.last[a] === n - 1 && A.cur[a] > 0) suf.push([a, A.cur[a]]); segs.push({ copy: c, n, pref, suf, best: Array.from(A.best) }); } return segs; } if (!isMainThread) { parentPort.postMessage(runCopyJobs(workerData)); } // ---- main ------------------------------------------------------------------- async function streamAnalyze(name, sab, Dbase, Wbase, pInner, pOuter, q, mode, Wouter, copyRange, partialPath) { const nCopies = mode === 'single' ? pInner : pOuter; const lo = copyRange ? copyRange[0] : 0, hi = copyRange ? copyRange[1] : nCopies; const jobs = Math.max(1, Math.min(JOBS, hi - lo)); const per = Math.ceil((hi - lo) / jobs); const ranges = []; for (let f = lo; f < hi; f += per) ranges.push([f, Math.min(hi, f + per)]); const t0 = Date.now(); const results = await Promise.all(ranges.map(([cFrom, cTo]) => new Promise((res, rej) => { const w = new Worker(new URL(import.meta.url), { workerData: { sab, Dbase, Wbase, pInner, pOuter, q, mode, cFrom, cTo, Wouter } }); w.on('message', (m) => res(m)); w.on('error', rej); }))); const secs = (Date.now() - t0) / 1000; const segs = []; for (const r of results) for (const s of r) segs.push(s); segs.sort((a, b) => a.copy - b.copy); if (partialPath) { // a chunk of the word: hand the raw segment summaries to the merge pass writeFileSync(partialPath, JSON.stringify({ name, q, mode, copyRange: [lo, hi], segments: segs, seconds: secs, workers: jobs }, null, 1) + '\n'); console.log('partial ' + partialPath + ': ' + segs.length + ' segments in ' + secs.toFixed(1) + ' s'); return null; } const best = mergeSegments(segs, q, true); const lad = laddersOf(best); const Dtot = segs.reduce((acc, s) => acc + s.n, 0); return { name, q, mode, D_old: Dtot, ...lad, best: Array.from(best), segments: segs.length, workers: jobs, seconds: secs }; } async function main(outPath) { const res = { tool: 'trunc-capladder/capladder.mjs', MAXRUN, JOBS, started: new Date().toISOString().replace(/\.\d+Z$/, 'Z') }; // base ladder, gated on the population counts. T_29's slot array (1.7 GB as // doubles) is never materialised: its gap/6 word is folded straight out of T_23's. const g6 = {}; const Dof = {}, G2of = {}; const Wof = {}; for (const top of [11, 13, 17, 19, 23]) { const t = buildTile(top); const g = gap6Of(t.slots, t.W); g6[top] = g.g6; Dof[top] = t.slots.length; G2of[top] = g.gmax; Wof[top] = t.W; } g6[29] = foldGap6(g6[23], Wof[23], 29); Dof[29] = g6[29].length; { let m = 0; for (let i = 0; i < g6[29].length; i++) if (g6[29][i] > m) m = g6[29][i]; G2of[29] = 6 * m; } Wof[29] = Wof[23] * 29; res.G1_tiles = {}; for (const top of [11, 13, 17, 19, 23, 29]) { res.G1_tiles[String(top)] = { D: Dof[top], published: D_PUB[top], agrees: Dof[top] === D_PUB[top], G2: G2of[top] }; } // #159's own published G2 for the folded word (its per-fold table) const G2_PUB = { 17: 108, 19: 150, 23: 204, 29: 258, 37: 528, 41: 546 }; res.G1_g2_check = {}; for (const top of [17, 19, 23, 29]) { res.G1_g2_check[String(top)] = { computed: G2of[top], published: G2_PUB[top], agrees: G2of[top] === G2_PUB[top] }; } // ---- the six enumerable folds, flat, plus L_star const folds = [[13, 17], [17, 19], [19, 23], [23, 29], [23, 31], [23, 37]]; res.FLAT = []; res.TIMINGS = {}; // wall times live outside the figures, so the // figures stay byte-comparable between runs for (const [x, q] of folds) { const t0 = Date.now(); const a = flatAnalyze(g6[x], q); const row = { fold: 'T' + x + ' by ' + q, x, q, ...a, alt_equals_anch: a.L_star_alt === a.L_anch, alt_equals_anch_plus_1: a.L_star_alt === a.L_anch + 1, alt_equals_free: a.L_star_alt === a.L_free, loose_equals_anch_plus_1: a.L_star_loose === a.L_anch + 1 }; res.TIMINGS[row.fold] = (Date.now() - t0) / 1000; res.FLAT.push(row); } // ---- grid: does the FREE ladder ever exceed the anchored one? res.GRID = {}; for (const x of [11, 13, 17, 19, 23]) { res.GRID['T' + x] = {}; for (const q of [13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) { if (q <= x) continue; const a = flatAnalyze(g6[x], q); res.GRID['T' + x][String(q)] = { L_anch: a.L_anch, L_free: a.L_free, aStar: a.aStar, L_star_alt: a.L_star_alt, L_star_loose: a.L_star_loose }; } } // ---- streamed gate: old word = T_23 generated from T_19 by 23, q = 31 { const sab19 = new SharedArrayBuffer(g6[19].length); new Uint8Array(sab19).set(g6[19]); const streamed = await streamAnalyze('gate T23 by 31', sab19, g6[19].length, Wof[19], 23, 0, 31, 'single', 0); const flat = flatAnalyze(g6[23], 31); res.G2_stream_vs_flat = { streamed: { L_anch: streamed.L_anch, L_free: streamed.L_free, D_old: streamed.D_old }, flat: { L_anch: flat.L_anch, L_free: flat.L_free, D_old: flat.D_old }, D_agrees: streamed.D_old === flat.D_old, ladders_agree: streamed.L_anch === flat.L_anch && streamed.L_free === flat.L_free, }; } // ---- T_29's gap word, the resident base for the two large folds const g29 = g6[29]; const sab29 = new SharedArrayBuffer(g29.length); new Uint8Array(sab29).set(g29); const W29 = Wof[29], W31 = W29 * 31; // ---- fold 31 -> 37: old word = T_31 = fold(T_29, 31), q = 37 if (HEAVY === 'both' || HEAVY === '31') { const s = await streamAnalyze('T31 by 37', sab29, g29.length, W29, 31, 0, 37, 'single', 0); res.STREAM = res.STREAM || {}; res.STREAM['T31 by 37'] = { ...s, L_star_alt: null, L_star_loose: null, D_old_published: D_PUB[31], D_agrees: s.D_old === D_PUB[31], note: 'L_star not computed at this fold; the flat folds establish L_star = L_free'}; writeFileSync('heavy-T31by37.json', JSON.stringify(res.STREAM['T31 by 37'], null, 1) + '\n'); console.log('T31 by 37:', JSON.stringify(res.STREAM['T31 by 37'])); } // ---- fold 37 -> 41: old word = T_37 = fold(T_31, 37), q = 41 // 2.5e11 inner steps, so it can be run in copy chunks and merged afterwards // (COPY_RANGE=a:b writes a partial; MERGE_37 stitches the partials). if (HEAVY === 'both' || HEAVY === '37') { const cr = process.env.COPY_RANGE ? process.env.COPY_RANGE.split(':').map(Number) : null; const partial = cr ? ('partial-37-' + cr[0] + '-' + cr[1] + '.json') : null; const s = await streamAnalyze('T37 by 41', sab29, g29.length, W29, 31, 37, 41, 'double', W31, cr, partial); if (s) { res.STREAM = res.STREAM || {}; res.STREAM['T37 by 41'] = { ...s, L_star_alt: null, L_star_loose: null, D_old_published: D_PUB[37], D_agrees: s.D_old === D_PUB[37], note: 'L_star not computed at this fold; the flat folds establish L_star = L_free'}; writeFileSync('heavy-T37by41.json', JSON.stringify(res.STREAM['T37 by 41'], null, 1) + '\n'); console.log('T37 by 41:', JSON.stringify(res.STREAM['T37 by 41'])); } } // ---- merge the copy-chunked partials of the 37 -> 41 fold if (process.env.MERGE_37 === '1') { const files = readdirSync('.').filter((f) => /^partial-37-\d+-\d+\.json$/.test(f)); const segs = []; for (const f of files) { const p = JSON.parse(readFileSync(f, 'utf8')); for (const s of p.segments) segs.push(s); } segs.sort((a, b) => a.copy - b.copy); const best = mergeSegments(segs, 41, true); const lad = laddersOf(best); const Dtot = segs.reduce((acc, s) => acc + s.n, 0); const out = { name: 'T37 by 41', q: 41, mode: 'double', D_old: Dtot, ...lad, best: Array.from(best), segments: segs.length, partials: files.length, D_old_published: D_PUB[37], D_agrees: Dtot === D_PUB[37], L_star_alt: null, L_star_loose: null, note: 'L_star not computed at this fold; the flat folds establish L_star = L_free' }; writeFileSync('heavy-T37by41.json', JSON.stringify(out, null, 1) + '\n'); console.log('T37 by 41:', JSON.stringify(out)); } res.finished = new Date().toISOString().replace(/\.\d+Z$/, 'Z'); writeFileSync(outPath, JSON.stringify(res, null, 1) + '\n', 'utf8'); console.log('wrote ' + outPath); } if (isMainThread) main(process.argv[2] || 'capladder.json');