#!/usr/bin/env python3 """Job #1910: LADDER reading -- census of T_29 folded by p = 31 (one full period P_31 = 31# = 200 560 490 130), i.e. the tile convention in which the fold is the next prime after the tile's own x. The T_29 word over [0,P_29) is built by folding T_23 by 29 with the shift-union identity (29 strips), kept as a cyclic gap array (uint16); residues mod 31 follow from a chunked exclusive cumsum, so no 6.5e9-position materialisation is needed. Controls: * D(T_29) = prod_{3<=q<=29}(q-2) = 214 708 725 and sum of gaps = P_29; * T_29 reconstruction from the gap array vs a direct sieve of coprime-to-29# residues on [0,2^24); * the FOLDED word at p = 31 vs an independently sieved T_31 = coprime-to-31# window (route 76's own control sentence); * kills = 2*D(T_29); measured adjacency count = Criterion prediction. """ import json, sys, time import numpy as np sys.path.insert(0, __file__.rsplit('/', 1)[0]) from fusion_census import build_tile, census_from_res, criterion from witness_and_controls import sieve_window P23 = 223092870 P29 = 29 * P23 P31 = 31 * P29 def build_t29_gaps(slots, Pm): """Cyclic gap array of T_29 over [0,P_29), by the shift-union fold of T_23.""" pieces = [] r29 = slots % 29 for k in range(29): a = (-k * Pm) % 29 keep = ~((r29 == a) | (r29 == (a - 2) % 29)) sq = slots[keep] if sq.size: pieces.append((k, sq)) D29 = int(sum(sq.size for _, sq in pieces)) gaps = np.empty(D29, dtype=np.uint16) idx = 0 prev_last = None r0 = None for k, sq in pieces: if prev_last is not None: gaps[idx] = Pm + int(sq[0]) - prev_last idx += 1 d = np.diff(sq) gaps[idx:idx + d.size] = d.astype(np.uint16) idx += d.size if r0 is None: r0 = int(sq[0]) prev_last = int(sq[-1]) # wrap gap: last survivor of strip 28 -> first survivor of strip 0 of the next period gaps[idx] = P29 - (28 * Pm + prev_last) + r0 idx += 1 assert idx == D29, (idx, D29) return gaps, r0, D29 def res_mod(gaps, r0, p): D = gaps.size res = np.empty(D, dtype=np.uint8) off = r0 % p step = 4_000_000 for s in range(0, D, step): ch = gaps[s:s + step].astype(np.int64) cum = np.cumsum(ch) exclusive = cum - ch res[s:s + ch.size] = ((off + exclusive) % p).astype(np.uint8) off = (off + int(cum[-1])) % p return res if __name__ == '__main__': t0 = time.time() slots, Pm, g23 = build_tile() gaps29, r0, D29 = build_t29_gaps(slots, Pm) out = { 'convention': 'ladder reading: tile T_29 (mod 29#), fold prime p = 31', 'P29': P29, 'P31': P31, 'D29': D29, 'D29_expected': 7952175 * 27, 'sum_gaps': int(gaps29.sum()), 'min_gap': int(gaps29.min()), 'max_gap': int(gaps29.max()), 'r0': r0, 'build_secs': round(time.time() - t0, 1), } # control: reconstruction near the start vs direct sieve of coprime-to-29# W = 1 << 24 cs = np.cumsum(gaps29[:1_200_000].astype(np.int64)) keep_n = int(np.searchsorted(cs, W)) recon = r0 + np.concatenate(([0], cs[:keep_n])) recon = recon[recon < W] direct29 = sieve_window(0, W, primes=(3, 5, 7, 11, 13, 17, 19, 23, 29)) out['t29_reconstruction_control'] = { 'window': [0, W], 'recon_count': int(recon.size), 'direct_count': int(direct29.size), 'identical': bool(recon.size == direct29.size and np.array_equal(recon, direct29)), } # control: folded word at p=31 vs independently sieved T_31 on the same window fold31 = np.array([x for x in direct29.tolist() if x % 31 not in (0, 29)], dtype=np.int64) direct31 = sieve_window(0, W, primes=(3, 5, 7, 11, 13, 17, 19, 23, 29, 31)) out['t31_window_control'] = { 'window': [0, W], 'folded_count': int(fold31.size), 'direct_T31_count': int(direct31.size), 'identical': bool(fold31.size == direct31.size and np.array_equal(fold31, direct31)), } # kill-class gaps and lemma regime p = 31 r = gaps29 % p cls = sorted(set(int(x) for x in gaps29[(r == 0) | (r == 2) | (r == p - 2)])) lim_lem = (p - 2) // 4 small = gaps29[gaps29 <= lim_lem] out['killclass_p31'] = { 'values_sorted_head': cls[:12], 'n_distinct': len(cls), 'lemma_limit': int(lim_lem), 'gaps_at_or_below_limit': int(small.size), 'those_in_kill_class': int(np.count_nonzero((small % p == 0) | (small % p == 2) | (small % p == p - 2))), } # criterion + census N0, Npm, pred_adj, tri, okc, t = criterion(gaps29, p) res = res_mod(gaps29, r0, p) out['res_build_secs'] = round(time.time() - t0, 1) hist, kills, jmax = census_from_res(res, D29, P29, p) adj = sum((j - 1) * v for j, v in hist.items()) j3 = sum(v for j, v in hist.items() if j >= 3) out['census'] = { 'period_positions': P31, 'slots_in_period': int(p * D29), 'kills': kills, 'kills_expected_2D': 2 * D29, 'gaps_j_ge_1': int(sum(hist.values())), 'hist': {str(k): int(v) for k, v in sorted(hist.items())}, 'j_ge_2': int(sum(v for j, v in hist.items() if j >= 2)), 'j_ge_3': int(j3), 'max_j': jmax, 'N0': N0, 'Npm': Npm, 'pred_adj': pred_adj, 'measured_adj': adj, 't': t, 'pred_j_ge_2': pred_adj - t, 'measured_j_ge_2': int(sum(v for j, v in hist.items() if j >= 2)), 'pred_has_j_ge_3': bool(t > 0), 'adjacency_prediction_exact': bool(adj == pred_adj), 'count_prediction_exact': bool(sum(v for j, v in hist.items() if j >= 2) == pred_adj - t), } out['total_secs'] = round(time.time() - t0, 1) print(json.dumps(out, indent=1))