"""Job #1505 (pursue route 45 rev 2). QUESTION (#711's next step): at N = 10^6, does the lambda-weighted truncated divisor exchange for Lambda(n-2) mu(n) reproduce return #708's 1/phi(d) portrait (truncated 1.662 x, full 2.282 x) within a pre-registered 5 % band, and does the integer identity still hold exactly at N = 2*10^4? OBJECTS. Exchange (E), exact for any f: sum_{n<=N} f(n-2) mu(n) = sum_{d<=N} (mu*mu)(d) sum_{m<=N/d} f(dm-2). Inner sum for f = Lambda: psi(N; d, -2) = sum_{m<=N/d} Lambda(dm-2). #708's portrait: remainder R(D) = sum_{d<=D, mu(d)!=0} (mu*mu)(d) (psi(N;d,-2) - main708(d)) with main708(d) = N/(d-1) for d > 1 and 0 for d = 1, reported as |R|/N: 1.662 (D = floor(N^{1/5}) = 15) and 2.282 (D = N). CARRIER WEIGHT (route 45 / #710): lambda_e = log(x/e) on odd e <= Q = floor(x/y), y = ceil(x^{12/25}); 0 otherwise. PRE-REGISTERED (from the route, before running): P1 the exact integer identity at N = 2*10^4 holds for the weighted form sum_n f(n-2) sum_{d|n} (mu*mu)(d) w_d = sum_d (mu*mu)(d) w_d sum_m f(dm-2) with an integer weight w_d = [d odd, d <= Q]; P2 |R_w(15)|/N within 5 % of 1.662 and |R_w(Q)|/N within 5 % of 2.282, where R_w uses lambda_d in place of 1 (the route's own band, taken literally); P3 (this job's addition, stated before running): the same quantities with the CORRECT heuristic main term main*(d) = N/phi(d) for odd d, N for d = 1, 0 for even d > 2 (dm-2 is even), because main708(d) = N/(d-1) is not N/phi(d) at composite d and is 0 at d = 1 where psi(N;1,-2) = psi(N-2) ~ N. CONTROLS: C1 reproduce #708's numbers to the printed precision (direct S = -1830.373179, truncated exchange -533216.765333, |R(15)|/N = 1.662, |R(N)|/N = 2.282, psi(N;2,-2) = 19 log 2); C2 the unweighted full exchange equals the direct sum to 1e-9 relative. Stdout: ledger + JSON. Runtime ~10 s. RUN: python exch1505.py --N 1000000""" import argparse, json, math, sys, time ap = argparse.ArgumentParser(); ap.add_argument('--N', type=int, default=10**6); ap.add_argument('--out', default='exch1505.json'); a = ap.parse_args() N = a.N; t0 = time.time(); led = [] def check(tag, ok, detail): led.append(f"{'PASS' if ok else 'FAIL'} {tag} :: {detail}"); print(led[-1], flush=True) # sieves mu = [1] * (N + 1); comp = bytearray(N + 1); primes = [] for i in range(2, N + 1): if not comp[i]: primes.append(i) for j in range(i, N + 1, i): mu[j] = -mu[j] for j in range(i * i, N + 1, i * i): mu[j] = 0 for j in range(i * i, N + 1, i): comp[j] = 1 mu[0] = 0 lam = [0.0] * (N + 1) for p in primes: pk = p while pk <= N: lam[pk] = math.log(p); pk *= p phi = list(range(N + 1)) for p in primes: for j in range(p, N + 1, p): phi[j] -= phi[j] // p # ghat = mu * mu (Dirichlet convolution) ghat = [0] * (N + 1) for d in range(1, N + 1): if mu[d] == 0: continue md = mu[d] for e in range(1, N // d + 1): ghat[d * e] += mu[e] * md print(f'sieves done ({time.time() - t0:.1f}s)', file=sys.stderr) y = math.ceil(N ** (12 / 25)); Q = N // y; D15 = int(math.floor(N ** 0.2)) def lam_w(d): return math.log(N / d) if (d % 2 == 1 and d <= Q) else 0.0 def psi(d): return sum(lam[d * m - 2] for m in range(1, N // d + 1)) def main708(d): return (N / (d - 1)) if d > 1 else 0.0 def main_star(d): if d == 1: return float(N) if d % 2 == 0: return 0.0 return N / phi[d] # P1: exact integer identity, weighted form, N = 2*10^4 CN = min(N, 20000); f = lambda k: ((k * k) % 101) - 50 ghatC = [0] * (CN + 1) for d in range(1, CN + 1): if mu[d] == 0: continue for e in range(1, CN // d + 1): ghatC[d * e] += mu[e] * mu[d] yC = math.ceil(CN ** (12 / 25)); QC = CN // yC w = lambda d: 1 if (d % 2 == 1 and d <= QC) else 0 G = [0] * (CN + 1) # G(n) = sum_{d|n} ghat(d) w_d for d in range(1, CN + 1): if ghatC[d] == 0 or w(d) == 0: continue for n in range(d, CN + 1, d): G[n] += ghatC[d] * w(d) lhs = sum(f(n - 2) * G[n] for n in range(3, CN + 1)) rhs = sum(ghatC[d] * w(d) * sum(f(d * m - 2) for m in range(-(-3 // d), CN // d + 1)) for d in range(1, CN + 1) if w(d)) check('P1_weighted_integer_identity', lhs == rhs, f'N={CN} Q={QC}: lhs={lhs} rhs={rhs}') lhs_u = sum(f(n - 2) * mu[n] for n in range(3, CN + 1)) rhs_u = sum(ghatC[d] * sum(f(d * m - 2) for m in range(-(-3 // d), CN // d + 1)) for d in range(1, CN + 1)) check('C0_unweighted_integer_identity_708', lhs_u == rhs_u and lhs_u == 1392, f'lhs={lhs_u} rhs={rhs_u} (#708: 1392)') # psi table for all squarefree d <= Q (and the full range for the D = N sums) psis = {} # squarefree d (the support #708's portrait used) psis_all = {} # every d with (mu*mu)(d) != 0 (the exchange identity needs squares too) for d in range(1, N + 1): if ghat[d] != 0: psis_all[d] = psi(d) if mu[d] != 0: psis[d] = psis_all[d] print(f'psi table done ({time.time() - t0:.1f}s)', file=sys.stderr) S = sum(lam[n - 2] * mu[n] for n in range(3, N + 1)) ex15 = sum(ghat[d] * psis_all[d] for d in psis_all if d <= D15) exfull = sum(ghat[d] * psis_all[d] for d in psis_all) R708_15 = sum(ghat[d] * (psis[d] - main708(d)) for d in psis if d <= D15) R708_N = sum(ghat[d] * (psis[d] - main708(d)) for d in psis) check('C1_reproduce_708', abs(S + 1830.373179) < 1e-5 and abs(ex15 + 533216.765333) < 1e-5 and abs(abs(R708_15) / N - 1.6619522580) < 1e-9 and abs(abs(R708_N) / N - 2.2824548089) < 1e-9 and abs(psis[2] - 19 * math.log(2)) < 1e-9, f'S={S:.6f} ex15={ex15:.6f} |R708(15)|/N={abs(R708_15)/N:.10f} |R708(N)|/N={abs(R708_N)/N:.10f} psi(2)={psis[2]:.10f}') check('C2_full_exchange_equals_direct', abs(exfull - S) / abs(S) < 1e-9, f'exfull={exfull:.10f} S={S:.10f}') # P2: weighted portrait with #708's main term, truncations D15 and Q Rw708_15 = sum(ghat[d] * lam_w(d) * (psis[d] - main708(d)) for d in psis if d <= D15) Rw708_Q = sum(ghat[d] * lam_w(d) * (psis[d] - main708(d)) for d in psis if d <= Q) r15, rQ = abs(Rw708_15) / N, abs(Rw708_Q) / N check('P2_band_truncated', abs(r15 / 1.662 - 1) <= 0.05, f'|R_w(15)|/N = {r15:.6f} vs 1.662 (ratio {r15/1.662:.3f})') check('P2_band_full', abs(rQ / 2.282 - 1) <= 0.05, f'|R_w(Q={Q})|/N = {rQ:.6f} vs 2.282 (ratio {rQ/2.282:.3f})') # normalised: divide the weighted remainder by the weighted main-term mass, so the log x scale of the weight cancels W15 = sum(abs(ghat[d]) * lam_w(d) for d in psis if d <= D15); WQ = sum(abs(ghat[d]) * lam_w(d) for d in psis if d <= Q) U15 = sum(abs(ghat[d]) for d in psis if d <= D15); UN = sum(abs(ghat[d]) for d in psis) # P3: corrected main term, unweighted and weighted Rs_15 = sum(ghat[d] * (psis[d] - main_star(d)) for d in psis if d <= D15) Rs_Q = sum(ghat[d] * (psis[d] - main_star(d)) for d in psis if d <= Q) Rs_N = sum(ghat[d] * (psis[d] - main_star(d)) for d in psis) Rws_15 = sum(ghat[d] * lam_w(d) * (psis[d] - main_star(d)) for d in psis if d <= D15) Rws_Q = sum(ghat[d] * lam_w(d) * (psis[d] - main_star(d)) for d in psis if d <= Q) absw_Q = sum(abs(ghat[d]) * lam_w(d) * abs(psis[d] - main_star(d)) for d in psis if d <= Q) # the carrier's absolute form abs_Q = sum(abs(ghat[d]) * abs(psis[d] - main_star(d)) for d in psis if d <= Q) contrib708 = {d: round(ghat[d] * (psis[d] - main708(d)), 3) for d in psis if d <= D15} Rs_all_15 = sum(ghat[d] * (psis_all[d] - main_star(d)) for d in psis_all if d <= D15) Rs_all_Q = sum(ghat[d] * (psis_all[d] - main_star(d)) for d in psis_all if d <= Q) Rs_all_N = sum(ghat[d] * (psis_all[d] - main_star(d)) for d in psis_all) per_d = {d: {'ghat': ghat[d], 'lambda': round(lam_w(d), 6), 'psi': round(psis[d], 3), 'main708': round(main708(d), 3), 'main_star': round(main_star(d), 3)} for d in psis if d <= D15} out = {'instrument': 'exch1505.py', 'job': 1505, 'N': N, 'y': y, 'Q': Q, 'D15': D15, 'direct_S': S, 'exchange_D15': ex15, 'exchange_full': exfull, 'R708_15_over_N': abs(R708_15) / N, 'R708_N_over_N': abs(R708_N) / N, 'Rw708_15_over_N': r15, 'Rw708_Q_over_N': rQ, 'signed': {'Rw708_15': Rw708_15, 'Rw708_Q': Rw708_Q, 'Rs_15': Rs_15, 'Rs_Q': Rs_Q, 'Rs_N': Rs_N, 'Rws_15': Rws_15, 'Rws_Q': Rws_Q}, 'corrected_main': {'Rs_15_over_N': Rs_15 / N, 'Rs_Q_over_N': Rs_Q / N, 'Rs_N_over_N': Rs_N / N, 'Rws_15_over_N': Rws_15 / N, 'Rws_Q_over_N': Rws_Q / N, 'abs_form_Q_over_N': abs_Q / N, 'abs_form_weighted_Q_over_N': absw_Q / N, 'weight_mass_15': W15, 'weight_mass_Q': WQ, 'count_mass_15': U15, 'count_mass_N': UN}, 'per_d_le_15': per_d, 'contrib708_15': contrib708, 'full_support': {'Rs_all_15_over_N': Rs_all_15 / N, 'Rs_all_Q_over_N': Rs_all_Q / N, 'Rs_all_N_over_N': Rs_all_N / N, 'n_d_with_ghat_nonzero': len(psis_all), 'n_squarefree': len(psis)}, 'ledger': led, 'secs': round(time.time() - t0, 1)} print(json.dumps({k: v for k, v in out.items() if k not in ('ledger', 'per_d_le_15')}, indent=1)) print('PER-D', json.dumps(per_d)); print('CONTRIB708_15', json.dumps(contrib708)); print('FULL_SUPPORT', json.dumps(out['full_support'])) json.dump(out, open(a.out, 'w'), indent=1)