#!/usr/bin/env python3 """0002 -- consolidated key numbers (single reproducible source for the programme). Produces research/0002/key_numbers.json with every finite number quoted in research-programme.md, at one consistent scale per section. """ import math, json from collections import defaultdict import numpy as np def isqrt_arr(m): r = np.sqrt(m.astype(np.float64)).astype(np.int64); r = np.maximum(r - 1, 0) while True: rr = r + 1; bad = rr * rr <= m if not bad.any(): break r[bad] = rr[bad] return r def k_of(m): return (isqrt_arr(m - 1) + 1) // 2 def ulam_xy(m): k = k_of(m); mm = m - (2 * k - 1) ** 2 x = np.where(mm <= 2*k, k, np.where(mm <= 4*k, 3*k - mm, np.where(mm <= 6*k, -k, mm - 7*k))) y = np.where(mm <= 2*k, mm - k, np.where(mm <= 4*k, k, np.where(mm <= 6*k, 5*k - mm, -k))) return np.where(m == 1, 0, x).astype(float), np.where(m == 1, 0, y).astype(float) def Psi_of(m): x1, y1 = ulam_xy(m); x2, y2 = ulam_xy(m + 2) d = np.arctan2(y2, x2) - np.arctan2(y1, x1) return (d + np.pi) % (2 * np.pi) - np.pi def omega_parity_table(n): spf = np.arange(n + 1) for i in range(2, math.isqrt(n) + 1): if spf[i] == i: spf[i*i::i] = np.minimum(spf[i*i::i], i) Om = np.zeros(n + 1, dtype=int) for m in range(2, n + 1): Om[m] = Om[m // spf[m]] + 1 return np.where(Om % 2 == 0, 1, -1) # lambda(n) def main(): X = 2_000_000 is_p = np.ones(X + 3, dtype=bool); is_p[:2] = False for i in range(2, math.isqrt(X + 2) + 1): if is_p[i]: is_p[i*i::i] = False lam = omega_parity_table(X + 2) n = np.arange(X - 1, dtype=np.int64) out = {} for z, Q in [(13, [3,5,7,11,13]), (19, [3,5,7,11,13,17,19])]: P = 1 for q in Q: P *= q ok = np.ones(X - 1, dtype=bool) for q in Q: ok &= (n % q != 0) & ((n + 2) % q != 0) ok &= (n % 2 == 1) # 2 is not in Q; twin-openers are odd adm = n[ok] tw = adm[is_p[adm] & is_p[adm + 2]] cp = adm[~tw_mask] if False else adm[~(is_p[adm] & is_p[adm + 2])] out[f"z{z}"] = { "P": P, "n_admissible": int(len(adm)), "n_twins": int(len(tw)), "n_composites": int(len(cp)), "twin_fraction": float(len(tw)/len(adm)), "lambda_prod_plus_on_twins": int((lam[tw]*lam[tw+2] == 1).sum()), "lambda_prod_minus_on_twins": int((lam[tw]*lam[tw+2] == -1).sum()), "lambda_prod_plus_on_comp": int((lam[cp]*lam[cp+2] == 1).sum()), "lambda_prod_minus_on_comp": int((lam[cp]*lam[cp+2] == -1).sum()), "matched_residue_pairs": int(sum(1 for t in tw.tolist() if ((cp % P) == (t % P)).any())), } # EXP-1 ring-conditioned numbers (Ulam), recomputed here for the record z, Q = 13, [3,5,7,11,13] P = 1 for q in Q: P *= q ok = np.ones(X - 1, dtype=bool) for q in Q: ok &= (n % q != 0) & ((n + 2) % q != 0) ok &= (n % 2 == 1) adm = n[ok] psi = Psi_of(adm); k = k_of(adm) tw = adm[is_p[adm] & is_p[adm + 2]]; cp = adm[~(is_p[adm] & is_p[adm + 2])] res_pairs = np.unique(np.stack([adm % P, np.round(psi, 6)], axis=1), axis=0) cnt = defaultdict(set) for r, v in res_pairs.tolist(): cnt[r].add(v) out["EXP1"] = { "X": X, "P": P, "residue_classes": len(cnt), "classes_with_multiple_Psi": int(sum(1 for v in cnt.values() if len(v) > 1)), "corr_k_Psi": round(float(np.corrcoef(k, psi)[0, 1]), 6), "n_twins": int(len(tw)), "n_composites": int(len(cp)), "twins_leaving_ring": int((k_of(tw + 2) != k_of(tw)).sum()), "comp_leaving_ring": int((k_of(cp + 2) != k_of(cp)).sum()), "min_abs_Psi_over_twins": float(np.abs(psi[is_p[adm] & is_p[adm+2]]).min()), } json.dump(out, open("key_numbers.json", "w"), indent=2) print(json.dumps(out, indent=2)) if __name__ == "__main__": main()