#!/usr/bin/env python3 """Job #1682 (route 60): fold T_x by a prime q and compare the true G2 of the folded tile with the L=1 certificate max_i (g_i + g_{i+1}); count qualifying old gaps (== 0, +-2 mod q); check the producer's L=1 transport N_new(t) <= (q-2) N(t) + 2 Q_1(t) on every integer t. T_x = {r mod x# : gcd(r(r+2), x#) = 1}. Folding by q: T_{x,q} = {r mod x#*q : r in T_x mod x#, r !== 0, -2 mod q}. Copies j = 0..q-1: position j*x# + t. Gaps of the folded tile are read copy by copy (the seam between copies j and j+1 is an ordinary gap because the tile is periodic and consecutive copies are adjacent). """ import numpy as np, sys, time, json def primes_upto(n): s = np.ones(n + 1, bool); s[:2] = False for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i*i::i] = False return np.nonzero(s)[0] def tile(x): P = int(np.prod([int(p) for p in primes_upto(x)])) ok = np.ones(P, bool) for p in primes_upto(x): p = int(p) ok[0::p] = False # r == 0 mod p ok[(p - 2) % p::p] = False # r == -2 mod p return P, np.nonzero(ok)[0].astype(np.int64) def gaps_of(pos, P): g = np.diff(pos) return np.append(g, pos[0] + P - pos[-1]) # cyclic closing gap def fold(P, pos, q): """Return (D_new, G2_new, gaps_new as a concatenated array over copies, in cyclic order).""" gaps_all = [] Pq = P * q keep_pos_first = None; last_pos = None G2 = 0; D = 0 for j in range(q): r = (j * P + pos) % q keep = (r != 0) & (r != (q - 2) % q) kp = j * P + pos[keep] D += kp.size if last_pos is not None: gaps_all.append(np.array([kp[0] - last_pos])) else: keep_pos_first = kp[0] gaps_all.append(np.diff(kp)) last_pos = kp[-1] gaps_all.append(np.array([keep_pos_first + Pq - last_pos])) g = np.concatenate(gaps_all) return D, int(g.max()), g def tail_counts(g, tmax): # N(t) = #{gaps >= t} for t = 0..tmax h = np.bincount(g, minlength=tmax + 2) return np.cumsum(h[::-1])[::-1][:tmax + 1] out = {} t0 = time.time() P19, T19 = tile(19) g19 = gaps_of(T19, P19) out['D19'] = int(T19.size); out['G2_19'] = int(g19.max()) print('T_19: D =', T19.size, ' G2 =', g19.max(), ' expect 378675 / 150') # blocking control: fold 19 by 23 must reproduce D(T_23) = 7952175 and G2 = 204 D23, G23, g23 = fold(P19, T19, 23) print('control 19>23: D_new =', D23, ' G2_new =', G23, ' expect 7952175 / 204') out['control_19_23'] = {'D': D23, 'G2': G23} assert D23 == 7952175 and G23 == 204 pair19 = int((g19 + np.roll(g19, -1)).max()) pair23 = int((g23 + np.roll(g23, -1)).max()) out['maxpair_19'] = pair19; out['maxpair_23'] = pair23 print('max adjacent pair: T_19 ->', pair19, ' T_23 ->', pair23) def qualifying(g, q): r = g % q return int(np.count_nonzero((r == 0) | (r == 2) | (r == q - 2))) results = [] for (x, P, pos, g, pair) in [(19, P19, T19, g19, pair19), (23, P19 * 23, None, g23, pair23)]: if pos is None: # rebuild T_23 positions from the fold (kept positions), needed to fold again pos = np.cumsum(np.concatenate([[T19[0]], g23[:-1]])).astype(np.int64) # first kept position of copy 0 of the 19>23 fold is T19[0] if it survives; recompute robustly: r0 = (T19) % 23; keep0 = (r0 != 0) & (r0 != 21); pos = np.cumsum(np.concatenate([[T19[keep0][0]], g23[:-1]])).astype(np.int64) for q in ([131, 139] if x == 19 else [127]): t1 = time.time() nq = qualifying(g, q) Dn, G2n, gn = fold(P, pos, q) tmax = max(int(gn.max()), pair) + 1 N = tail_counts(g, tmax); Nn = tail_counts(gn, tmax) pairs = g + np.roll(g, -1) Q1 = tail_counts(pairs, tmax) rhs = (q - 2) * N + 2 * Q1 viol = int(np.count_nonzero(Nn[1:] > rhs[1:])) plain_viol = int(np.count_nonzero(Nn[1:] > (q - 2) * N[1:])) cert = max(int(g.max()), pair) row = {'x': x, 'q': q, 'qualifying_old_gaps': nq, 'D_new': int(Dn), 'G2_new': G2n, 'certificate_L1': cert, 'tight': G2n == cert, 'violations_L1_form': viol, 'violations_plain_form': plain_viol, 'twoq_minus2_gt_G2old': 2 * q - 2 > int(g.max()), 'seconds': round(time.time() - t1, 1)} results.append(row); print(row) out['folds'] = results out['seconds_total'] = round(time.time() - t0, 1) # comparison for 23>29/31/37: the L=1 pair certificate vs the exact ladder (L>=2 windows must supply the rest) out['pair23_vs_ladder'] = {'maxpair_23': pair23, 'G2_29': 258, 'G2_31': 348, 'G2_37': 528, 'L1_alone_suffices_at_29': pair23 >= 258, 'at_31': pair23 >= 348, 'at_37': pair23 >= 528} print(out['pair23_vs_ladder']) json.dump(out, open('fold1682.json', 'w'), indent=1) print('total seconds', out['seconds_total'])