#!/usr/bin/env python3 """Job #1007 — the end/interior split measured over EVERY attaining position. Route 10 asks: is the split of the twin-Jacobsthal record a property of the level, or the noise of one witness chosen out of up to 20 attaining positions? The ancestry test is the served merge-test.py one (sha fd9a7f5c...), re-stated here so that it can be applied to a set of positions instead of one; §gate re-derives the served output from the served positions and fails loudly if this copy disagrees. Usage: python splits.py gate # reproduce the served split table for the 9 certified transitions python splits.py table # merge the shard outputs, split every attaining position python splits.py witnesses # step A: split BOTH 43# witnesses """ import glob import json import os import sys from math import gcd PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] TRANSITIONS = {13: 11, 17: 13, 19: 17, 23: 19, 29: 23, 31: 29, 37: 31, 41: 37, 43: 41} # the corpus's certified custody record: level -> (A_1(x), certified start of the record gap) LADDER = {11: (42, 899), 13: (66, 731), 17: (108, 701), 19: (150, 659), 23: (204, 76166567), 29: (258, 1205437109), 31: (348, 8813641451), 37: (528, 544899485411), 41: (546, 3784200788231), 43: (618, 830330079152051)} # #401 recovered the second 43# witness from the staged chunk table WITNESS_43 = [830330079152051, 1403312099425139] def primorial(p): r = 1 for q in PRIMES: if q <= p: r *= q return r def slots_between(p, lo, hi): """T_p slots in [lo, hi]: n with gcd(n(n+2), p#) = 1. Span <= 618, so trial division is exact.""" P = primorial(p) return [n for n in range(lo, hi + 1) if gcd(n * (n + 2), P) == 1] def analyse(x, p, pos, g): """Split one attaining T_x gap into T_p gaps; the served merge-test.py convention. The maximality of the gap is re-checked DIRECTLY here (no interior integer is a T_x slot), so a position surviving this function is a genuine gap of length g at level x independently of the enumerator that offered it. """ P = primorial(x) assert gcd(pos * (pos + 2), P) == 1, "start is not a T_x slot" assert gcd((pos + g) * (pos + g + 2), P) == 1, "end is not a T_x slot" assert all(gcd(n * (n + 2), P) != 1 for n in range(pos + 1, pos + g)), \ "an interior integer is a T_x slot: the gap is shorter than g" s = slots_between(p, pos, pos + g) assert s[0] == pos and s[-1] == pos + g, "endpoints are not T_p slots" gaps = [s[i + 1] - s[i] for i in range(len(s) - 1)] mod = 6 * x c = (6 * (2 * pow(6, -1, x) % x)) % mod interior = gaps[1:-1] return dict(x=x, p=p, g=g, pos=pos, mod=mod, c=c, gaps=gaps, L=len(gaps), interior=interior, ends=(gaps[0] + gaps[-1]), sum_interior=sum(interior), share=sum(interior) / g, qualifies=all(q % mod in (0, c, mod - c) for q in interior), n_dead=sum(1 for n in s[1:-1] if gcd(n * (n + 2), P * x) != 1)) def gate(): """Reproduce the served merge-test.out rows from the served positions.""" # the served table rows: x p A_1(x) L deg [gaps] import re want = {} for line in open('job1007/merge-test.out', encoding='utf-8'): m = re.match(r'\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\[.*\])', line) if m: want[int(m.group(1))] = dict(p=int(m.group(2)), g=int(m.group(3)), L=int(m.group(4)), gaps=json.loads(m.group(6))) rows = [] for x in sorted(TRANSITIONS): if x not in want: continue r = analyse(x, TRANSITIONS[x], LADDER[x][1], LADDER[x][0]) ok = (r['gaps'] == want[x]['gaps'] and r['L'] == want[x]['L'] and r['qualifies']) rows.append((x, ok, r['gaps'], want[x]['gaps'])) bad = [r for r in rows if not r[1]] print('gate: %d/%d served transitions reproduced (gaps, L, class)' % (len(rows) - len(bad), len(rows))) for x, ok, got, exp in bad: print(' x=%d MISMATCH got %s want %s' % (x, got, exp)) return 0 if not bad else 1 def merge_shards(x): """Combine one level's shard outputs into the full attaining set.""" files = sorted(glob.glob('job1007/max-%d-*.json' % x)) if not files: return None shards = [] for f in files: if os.path.getsize(f) == 0: return None shards.append(json.load(open(f, encoding='utf-8'))) shards.sort(key=lambda d: d['shard']) P = shards[0]['P'] best = max([g for d in shards for g, _ in d['top']] or [0]) # gaps that straddle a shard boundary for a, b in zip(shards, shards[1:]): best = max(best, b['firstSlot'] - a['lastSlot']) best = max(best, shards[0]['firstSlot'] + P - shards[-1]['lastSlot']) starts = [s for d in shards for g, s in d['top'] if g == best] for a, b in zip(shards, shards[1:]): if b['firstSlot'] - a['lastSlot'] == best: starts.append(a['lastSlot']) if shards[0]['firstSlot'] + P - shards[-1]['lastSlot'] == best: starts.append(shards[-1]['lastSlot']) return dict(x=x, P=P, G2=best, slots=sum(d['slotCount'] for d in shards), expected_slots=primorial_slots(x), starts=sorted(set(starts)), shards=len(shards)) def primorial_slots(x): r = 1 for p in PRIMES: if 2 < p <= x: r *= (p - 2) return r def emit(name, text): """Write an artifact with LF endings whatever the platform, so the sha256 matches elsewhere.""" with open('job1007/' + name, 'w', encoding='utf-8', newline='\n') as fh: fh.write(text) def table(): out = {} lines = [] def say(s): print(s) lines.append(s) for x in [11, 13, 17, 19, 23, 29, 31]: m = merge_shards(x) if m is None: say('missing shards for x=%d' % x) continue if m['slots'] != m['expected_slots']: say('x=%d SLOT COUNT GATE FAILED: %d != %d' % (x, m['slots'], m['expected_slots'])) continue p = x if x == 11 else prev_prime(x) rows = [analyse(x, p, s, m['G2']) for s in m['starts']] shares = [r['share'] for r in rows] g, certified = LADDER.get(x, (None, None)) least_match = (m['G2'] == g and min(m['starts']) == certified) out[x] = dict(G2=m['G2'], mult=len(rows), Ls=sorted({r['L'] for r in rows}), shares=shares, min=min(shares), max=max(shares), mean=sum(shares) / len(shares), weighted=sum(r['sum_interior'] for r in rows) / (len(rows) * m['G2']), all_qualify=all(r['qualifies'] for r in rows), least_matches_record=least_match, slots=m['slots'], expected_slots=m['expected_slots'], starts=m['starts'], rows=rows) say(' gate: slots %d = prod(p-2) %s | least start %d vs record %s %s' % (m['slots'], m['expected_slots'] == m['slots'], min(m['starts']), certified, 'MATCH' if least_match else 'MISMATCH')) say('x=%-3d A_1=%-4d mult=%-3d L in %-9s shares %s spread %.3f mean %.3f class %s' % (x, m['G2'], len(rows), sorted({r['L'] for r in rows}), ['%.3f' % s for s in shares], max(shares) - min(shares), sum(shares) / len(shares), 'holds' if out[x]['all_qualify'] else 'FAILS')) with open('job1007/splits.json', 'w', encoding='utf-8', newline='\n') as fh: json.dump(out, fh, indent=1, sort_keys=True) emit('splits.out', '\n'.join(lines) + '\n') return out def prev_prime(x): ps = [p for p in PRIMES if p < x] return ps[-1] def witnesses(): """Step A: every witness the record holds at levels whose full position set is out of reach.""" rows, lines = [], [] def say(s): print(s) lines.append(s) for pos in WITNESS_43: r = analyse(43, 41, pos, LADDER[43][0]) rows.append(r) say('x=43 pos=%d A_1=618 L=%d gaps=%s ends=%d interior=%d share=%.3f class=%s' % (pos, r['L'], r['gaps'], r['ends'], r['sum_interior'], r['share'], r['qualifies'])) p, q = (r['share'] for r in rows) say('43# witness spread: %.3f (%.3f .. %.3f of the 618; the record has 8 attaining positions)' % (abs(p - q), min(p, q), max(p, q))) for x in (37, 41): g, pos = LADDER[x] r = analyse(x, prev_prime(x), pos, g) rows.append(r) say('x=%d pos=%d A_1=%d L=%d gaps=%s ends=%d interior=%d share=%.3f class=%s ' '(ONE of the record\'s %s attaining positions)' % (x, pos, g, r['L'], r['gaps'], r['ends'], r['sum_interior'], r['share'], r['qualifies'], {37: 2, 41: 4}[x])) with open('job1007/witnesses.json', 'w', encoding='utf-8', newline='\n') as fh: json.dump(rows, fh, indent=1, sort_keys=True) emit('witnesses.out', '\n'.join(lines) + '\n') if __name__ == '__main__': cmd = sys.argv[1] if len(sys.argv) > 1 else 'gate' if cmd == 'gate': sys.exit(gate()) elif cmd == 'table': table() elif cmd == 'witnesses': witnesses()