#!/usr/bin/env python3 """Exact small-divisor CRT moments, one fiber at a time, without walking p#.""" import csv import json import math import resource import sys import time from pathlib import Path def primes_to(p): return [q for q in range(2, p + 1) if all(q % d for d in range(2, math.isqrt(q) + 1))] def next_prime(p): q = p + 1 while not all(q % d for d in range(2, math.isqrt(q) + 1)): q += 1 return q def balanced(M, N): assert M >= 0 and N >= 1 a, r = divmod(M, N) return (N - r) * a * a + r * (a + 1) ** 2 def kernel(qs, h): return math.prod(q - len({0, (-2) % q, (-h) % q, (-h - 2) % q}) for q in qs) def run(config, output): rows, summaries = [], [] separated = 0 for p in config['prime_heads']: qs = primes_to(p) W = math.prod(qs) D = math.prod(q - (1 if q == 2 else 2) for q in qs) successor = next_prime(p) top = successor * successor - p - 3 assert 1 <= top < W whole_k = [kernel(qs, h) for h in range(top)] for m in config['divisors']: assert W % m == 0 N = W // m assert N >= 3 residual = [q for q in qs if m % q] K = math.prod(q - (1 if q == 2 else 2) for q in residual) kh = [kernel(residual, h) for h in range(top)] local = [int(math.gcd(a * (a + 2), m) == 1) for a in range(m)] moments = [] for a in range(m): # The new endpoint contributes K to M and two times its # pair correlations to S2. Keep only two prefix moments; # there is no large table of offset-pair prefixes. alive, M, pairs, prefix = [], 0, 0, [(0, 0)] for j in range(top): if local[(a + j) % m]: M += K pairs += sum(kh[j - i] for i in alive) alive.append(j) prefix.append((M, M + 2 * pairs)) moments.append(prefix) first_target = first_all = first_separation = None target_count = all_count = separation_count = 0 a0 = (p + 1) % m for L in range(1, top + 1): ms = [moments[a][L][0] for a in range(m)] ss = [moments[a][L][1] for a in range(m)] assert sum(ms) == L * D whole_s2 = L * D + 2 * sum((L - h) * whole_k[h] for h in range(1, L)) assert sum(ss) == whole_s2 margins = [] for a in range(m): b = (-a - L - 1) % m assert (ms[a], ss[a]) == (ms[b], ss[b]) z = 2 if L >= 6 and a == b else 1 margins.append(ss[a] - balanced(ms[a], N - z)) target_pass = margins[a0] < 0 all_pass = max(margins) < 0 assert not all_pass or target_pass target_count += target_pass all_count += all_pass if target_pass and first_target is None: first_target = L if all_pass and first_all is None: first_all = L if target_pass and not all_pass: separation_count += 1 if first_separation is None: first_separation = L z0 = 2 if L >= 6 and a0 == (-a0 - L - 1) % m else 1 rows.append([p, successor, m, W, D, N, L, a0, z0, ms[a0], ss[a0], margins[a0], max(margins), sum(v >= 0 for v in margins)]) summaries.append({'p': p, 'm': m, 'last_L': top, 'first_target_pass': first_target, 'first_all_pass': first_all, 'first_separation': first_separation, 'target_passes': target_count, 'all_passes': all_count, 'separations': separation_count}) separated += separation_count with output.open('w', newline='') as f: writer = csv.writer(f, lineterminator='\n') writer.writerow(['p', 'next_prime', 'm', 'W', 'D', 'fiber_size', 'L', 'target_residue', 'target_zero_budget', 'target_M', 'target_S2', 'target_margin', 'max_fiber_margin', 'failed_fibers']) writer.writerows(rows) print(json.dumps({'rows': len(rows), 'controls': 'passed', 'target_only_points': separated, 'summaries': summaries}, separators=(',', ':'))) if __name__ == '__main__': config = json.loads(Path(sys.argv[1]).read_text()) cap = config['resources']['cpu_seconds'] resource.setrlimit(resource.RLIMIT_CPU, (cap, cap)) started = time.process_time() run(config, Path(sys.argv[2])) usage = resource.getrusage(resource.RUSAGE_SELF) # macOS uses bytes, Linux uses KiB for ru_maxrss. Timing is not a target. rss = usage.ru_maxrss if sys.platform == 'darwin' else usage.ru_maxrss * 1024 assert rss < config['resources']['ram_bytes'], 'RAM cap exceeded' assert Path(sys.argv[2]).stat().st_size < config['resources']['disk_bytes'] print(json.dumps({'cpu_seconds': time.process_time() - started, 'max_rss_bytes': rss}), file=sys.stderr)