#!/usr/bin/env python3 """Job #1028 -- fold 29 as a CLOSED phase, without a 29# = 6.47e9 byte array. The combined slot/dead pattern of a fold has period x#: the slots repeat mod M = (x-1)# and the dead condition depends on the residue mod x. A window cut out of that phase is not a closed system, so cyclically identifying its edges invents a seam where the dead pattern jumps - which is why the fold-29 window in this job's skeleton.py cannot test the supply identity of Claim 5. The pattern factorises, though. If idx lists the T_(x-1) slots of one period [0, M), the slots of period k are idx + k*M, so **every period has the same gap array** (diff(idx), plus the constant cross-period gap idx[0] + M - idx[-1]) and only the dead flags move: a slot of period k is dead iff its residue mod x lies in the 2-set {-k*M, -k*M - 2}. As k runs over the x periods those sets run over all x of them - the record's fold picture - so the whole phase is x cheap rotations of one period. Each rotation is walked over THREE periods (k-1, k, k+1) with runs kept by start index in the middle period, which counts every run of the phase exactly once and none at an array edge. Verified per run: (T1) every inner gap is in C_x; (T2) the number of qualifying gaps of the level-x gap is (r-1) + q1 + q2 with the boundary pairing of the return; and the supply split Nq = X + Q + F with Z, the number of inner gaps in the 0-class - the fourth closed phase testing Claim 5's relation Q = 2*(X - Z). Usage: python fold29.py (fold x = 29; the constant X below names it) Output: JSON on stdout, progress on stderr. Stdlib + numpy. """ import json import sys import numpy as np X = 29 PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] LADDER = {29: 258} def primorial(x): r = 1 for p in PRIMES: if p <= x: r *= p return r def class_c(x): return (6 * (2 * pow(6, -1, x) % x)) % (6 * x) def main(): x = X M = primorial(x - 1) alive = np.ones(M, dtype=bool) for p in (q for q in PRIMES if q < x): alive[0::p] = False alive[(p - 2) % p::p] = False idx = np.flatnonzero(alive) N = len(idx) mod = 6 * x c = class_c(x) cls = np.array([0, c, mod - c], dtype=np.int64) gaps = np.concatenate([np.diff(idx), [idx[0] + M - idx[-1]]]) # N cyclic gaps, one per slot gq = np.isin(gaps % mod, cls) # gap j is in C_x gz = gaps % mod == 0 sys.stderr.write("fold %d: M=%-11d slots/period=%-9d phase slots=%-11d c=%-4d mod=%d\n" % (x, M, N, x * N, c, mod)) cum = np.concatenate(([0], np.cumsum(np.concatenate([gq, gq, gq])))) X_inner = Z_zero = Q_bnd = F_free = Nq = 0 T1 = T2 = BND = 0 runs = 0 r_max = 0 g_max = 0 hist = {} aa_max = 0 for k in range(x): # the three periods are the ones at offsets k-1, k, k+1; positions and residues must use the # SAME offsets (a mismatch here shifts one period and was caught by the k=1 rotation) off = [(k - 1) % x, k, (k + 1) % x] idx3 = np.concatenate([idx + o * M for o in off]) d3 = np.concatenate([(idx + o * M) % x for o in off]) dead3 = (d3 == 0) | (d3 == x - 2) # ---- aggregate over the N pairs whose LEFT slot lies in the middle period ------------- j = N + np.arange(N) # pair j joins slot j, j+1 da, db = dead3[j], dead3[j + 1] a1p, a2p = idx3[j], idx3[j + 1] both = da & db one = da ^ db free = ~(da | db) gpair = a2p - a1p qpair = np.isin(gpair % mod, cls) X_inner += int(both.sum()) Z_zero += int((both & (gpair % mod == 0)).sum()) Q_bnd += int((one & qpair).sum()) F_free += int((free & qpair).sum()) Nq += int(qpair.sum()) aa_max = max(aa_max, int(gpair[free].max()) if free.any() else 0) # ---- runs: maximal dead stretches, kept by start index in the middle period ----------- di = np.diff(dead3.view(np.int8)) S = np.flatnonzero(di == 1) + 1 E = np.flatnonzero(di == -1) + 1 if dead3[0]: # a run touching index 0 has no False->True transition E = E[1:] if dead3[-1]: # a run touching the last index has no True->False transition S = S[:-1] assert len(S) == len(E) keep = (S >= N) & (S < 2 * N) # starts inside the middle period S, E = S[keep], E[keep] if len(S) == 0: continue assert (S < E).all() and (E < 3 * N).all() runs += len(S) a1, a2 = idx3[S - 1], idx3[E] r = E - S r_max = max(r_max, int(r.max())) b1, b2 = idx3[S] - a1, a2 - idx3[E - 1] j0 = S % N j1 = (E - 1) % N inner_cnt = np.where(j1 >= j0, cum[N + j1] - cum[N + j0], cum[N + N] - cum[N + j0] + cum[N + j1]) b1in = gq[(S - 1) % N] b2in = gq[(E - 1) % N] T1 += int((inner_cnt != (r - 1)).sum()) Eobs = inner_cnt + b1in + b2in d1, dr = idx3[S] % x, idx3[E - 1] % x q1 = ((d1 == 0) & (a1 % x == 2)) | ((d1 == x - 2) & (a1 % x == x - 4)) q2 = ((dr == 0) & (a2 % x == 2)) | ((dr == x - 2) & (a2 % x == x - 4)) T2 += int((Eobs != (r - 1) + q1 + q2).sum()) BND += int((b1in != q1).sum() + (b2in != q2).sum()) g = a2 - a1 g_max = max(g_max, int(g.max())) for kk, vv in zip(*np.unique(r, return_counts=True)): hist[int(kk)] = hist.get(int(kk), 0) + int(vv) res = dict(fold=x, period_slots=N, phase_slots=x * N, runs=runs, r_max=r_max, max_level_gap=g_max, max_active_active_gap=aa_max, inner_X=X_inner, inner_zero_Z=Z_zero, boundary_positions=X_inner + Q_bnd + F_free, boundary_qualifying_Q=Q_bnd, free_F=F_free, Nq=Nq, identity_Q_eq_2XmZ=bool(Q_bnd == 2 * (X_inner - Z_zero)), two_XmZ=2 * (X_inner - Z_zero), violations_T1=T1, violations_T2=T2, violations_boundary=BND, r_hist={str(k): hist[k] for k in sorted(hist)}) res["A1_gate_ok"] = (g_max == LADDER[x]) sys.stderr.write("fold %d CLOSED phase: runs=%d r_max=%d max level-x gap=%d (served A_1 %d, match=%s)\n" % (x, runs, r_max, g_max, LADDER[x], res["A1_gate_ok"])) sys.stderr.write(" X(inner)=%d Z(class-0 inner)=%d Q(boundary qualifying)=%d 2(X-Z)=%d Q=2(X-Z): %s\n" % (X_inner, Z_zero, Q_bnd, 2 * (X_inner - Z_zero), res["identity_Q_eq_2XmZ"])) sys.stderr.write(" Nq=%d = X %d + Q %d + F %d violations: T1=%d T2=%d boundary=%d\n" % (Nq, X_inner, Q_bnd, F_free, T1, T2, BND)) print(json.dumps(res, indent=1)) if __name__ == "__main__": main()