#!/usr/bin/env node 'use strict'; // ============================================================================ // census.js -- replace a random sample with an exhaustive census. // // Return #12's probe (`runfor-probe.js`, served) asks whether the greedy 2-set // scanner `runFor` is exact, by testing 7 listed cases and then drawing random // residue sequences at p = 11 (n = 4 + (t mod 4)) until 3 disagreements appear. // Return #191 seeded that probe so its stdout stops moving; the number it now // freezes is ONE DRAW, not a rate. // // At p = 11 and n <= 7 the whole space is 11^4 + 11^5 + 11^6 + 11^7 = 21,434,424 // sequences, so the rate is exactly computable. This script does that, and then // answers the sampling question: how variable is "the first 3 disagreements"? // // runFor and exact are EXTRACTED VERBATIM from the served probe file (slice of // source, evaluated) and the listed-case table is reprinted as the gate: if my // extraction were not the served code, the 7 lines would differ from the probe's // own stdout. // // Usage: PROBE= node census.js [nSeeds] // stdout: deterministic JSON. Progress/timing: stderr. // ============================================================================ const fs = require('fs'); const PROBE = process.env.PROBE || 'job996/files/probe-seeded.js'; const NSEEDS = parseInt(process.argv[2], 10) || 200; const src = fs.readFileSync(PROBE, 'utf8'); const a = src.indexOf('function runFor'); const b = src.indexOf('// mulberry32'); if (a < 0 || b < 0 || b <= a) throw new Error('cannot locate runFor/exact in ' + PROBE); const { runFor, exact } = new Function(src.slice(a, b) + '\nreturn { runFor, exact };')(); const P = 11; const cases = [ [[1, 3, 3, 5], 11], [[1, 3, 3, 3, 5], 11], [[5, 3, 3, 1], 11], [[0, 2, 2, 4], 13], [[1, 3, 3, 5, 5, 7], 11], [[2, 2, 4, 4, 6], 13], [[1, 1, 3, 3, 5], 11], ]; const listed = cases.map(([S, p]) => ({ p, S: S.slice(), runFor: runFor(S, p), exact: exact(S, p), disagree: runFor(S, p) !== exact(S, p) })); process.stderr.write('gate: the 7 listed cases (compare with the probe\'s own stdout)\n'); for (const c of listed) process.stderr.write(' ' + JSON.stringify(c) + '\n'); // ---------------------------------------------------------------- exhaustive census function pow(base, e) { let v = 1; for (let i = 0; i < e; i++) v *= base; return v; } const census = {}; for (let n = 4; n <= 7; n++) { const total = pow(P, n); const S = new Array(n).fill(0); let bad = 0, worstDeficit = 0, first = null; const t0 = Date.now(); for (let idx = 0; idx < total; idx++) { const rf = runFor(S, P), ex = exact(S, P); if (rf !== ex) { bad++; if (ex - rf > worstDeficit) worstDeficit = ex - rf; if (first === null) first = S.slice(); } // odometer, least significant last (matches the probe's drawing order) for (let k = n - 1; k >= 0; k--) { if (++S[k] < P) break; S[k] = 0; } if ((idx & 0xFFFFF) === 0 && idx) { process.stderr.write(` n=${n}: ${((idx / total) * 100).toFixed(0)}% [${((Date.now() - t0) / 1000).toFixed(0)}s]\n`); } } census[n] = { sequences: total, disagreements: bad, rate_num: bad, rate_den: total, worst_deficit: worstDeficit, first_witness: first }; process.stderr.write(` n=${n}: ${bad}/${total} disagree (${((bad / total) * 100).toFixed(4)}%)\n`); } // -------------------------------------------------- how variable is "the first 3"? // The probe draws n = 4 + (t mod 4) each iteration and stops at the 3rd disagreement // (or at t = 400000). Exact per-n success probabilities give the exact law of the // stopping index by a 16-state chain; replaying the seeded generator gives one path. const r = {}; for (let n = 4; n <= 7; n++) r[n] = census[n].disagreements / census[n].sequences; function expectedStopping() { // states: found 0..3 (3 = absorbing) times phase t mod 4 let dist = new Array(16).fill(0); dist[0] = 1; let mean = 0, varianceSum = 0, p95 = null, cum = 0; for (let t = 0; t < 2000000; t++) { const p = r[4 + (t % 4)]; const next = new Array(16).fill(0); let remaining = 0; for (let ph = 0; ph < 4; ph++) { for (let f = 0; f < 3; f++) { const m = dist[f * 4 + ph]; if (!m) continue; const f2 = Math.min(3, f + 1); next[f2 * 4 + ((ph + 1) % 4)] += m * p; next[f * 4 + ((ph + 1) % 4)] += m * (1 - p); } } const stopped = dist[12] + dist[13] + dist[14] + dist[15]; mean += (t + 1) * stopped; varianceSum += (t + 1) * (t + 1) * stopped; if (p95 === null && stopped > 0) { cum += stopped; if (cum >= 0.5 && p95 === null) p95 = t + 1; } dist = next; remaining = dist.reduce((s, v) => s + v, 0); if (remaining < 1e-12) break; } const sd = Math.sqrt(Math.max(0, varianceSum - mean * mean)); return { mean_iterations: Number(mean.toFixed(1)), sd_iterations: Number(sd.toFixed(1)), median_iterations: p95 }; } function replay(seed) { let s = seed | 0; const rand = function () { s = (s + 0x6D2B79F5) | 0; let t = s; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; let found = 0; for (let t = 0; t < 400000 && found < 3; t++) { const p = 11, n = 4 + (t % 4), S = []; for (let i = 0; i < n; i++) S.push(Math.floor(rand() * p)); if (runFor(S, p) !== exact(S, p)) found++; if (found === 3) return t + 1; } return found === 3 ? 400000 : -1; } const sweep = []; for (let seed = 1; seed <= NSEEDS; seed++) sweep.push(replay(seed)); sweep.sort((x, y) => x - y); const q = (f) => sweep[Math.min(sweep.length - 1, Math.floor(f * sweep.length))]; const out = { probe: PROBE, extraction_gate_listed_cases: listed, p: P, census, rates: r, stopping_law_analytic: expectedStopping(), seed_sweep: { seeds: [1, NSEEDS], n: sweep.length, min: sweep[0], q25: q(0.25), median: q(0.5), q75: q(0.75), max: sweep[sweep.length - 1], reported_seed_12345: replay(12345), }, }; process.stdout.write(JSON.stringify(out, null, 1) + '\n'); process.stderr.write('done\n');