#!/usr/bin/env python3 """job #1501 — exact finite check of the divisor-exchange identity for the fixed-shift product a(n) = Lambda(n-2) mu(n), plus a portrait of where the shift goes. Identity (exact, for ANY f and g with g(n) = sum_{d|n} ghat(d), ghat = g * mu): sum_{n<=N} f(n-2) g(n) = sum_{d<=N} ghat(d) * sum_{m<=N/d} f(d*m - 2). Two independent checks: A. integer-valued f -> exact integer equality (no rounding at all); B. f = Lambda -> the arithmetic identity in floats, and the truncated form at D = floor(N^(1/5)), the level #1497/#1498 name for the input class. Everything printed to stdout; the caller redirects to out.log (sah.py exec truncates at 4000 B, so this never runs through exec's stdout capture). """ import json, math, sys, time T0 = time.time() def sieve_mu_upto(n): mu = [1] * (n + 1) primes = [] comp = bytearray(n + 1) 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] sq = i * i for j in range(sq, n + 1, sq): mu[j] = 0 for j in range(i * i, n + 1, i): comp[j] = 1 mu[0] = 0 return mu, primes def sieve_lambda_upto(n, primes): lam = [0.0] * (n + 1) for p in primes: lp = math.log(p) pk = p while pk <= n: lam[pk] = lp # Lambda(j) = log p exactly when j = p^k, else 0 if pk > n // p: break pk *= p return lam def dirichlet_inverse_pair(g, n, mu): """return ghat with ghat * 1 = g, i.e. ghat = g * mu, computed by the divisor double loop.""" ghat = [0] * (n + 1) for d in range(1, n + 1): if mu[d] == 0: continue m = mu[d] for e in range(1, n // d + 1): ghat[d * e] += g[e] * m return ghat N = int(sys.argv[1]) if len(sys.argv) > 1 else 10 ** 6 CHECK_N = min(N, 20000) # exact-integer check range D_SMALL = int(math.floor(N ** 0.2)) # x^(1/5) mu, primes = sieve_mu_upto(N) lam = sieve_lambda_upto(N, primes) out = {"script": "job1501-shift-exchange.py", "N": N, "D_x_pow_1_5": D_SMALL, "checks": []} def check(name, ok, detail): out["checks"].append({"name": name, "ok": bool(ok), "detail": detail}) print(("PASS " if ok else "FAIL ") + name + "\n " + detail) # ---------------------------------------------------------------- A. exact integer check # f(k) = ((k*k) % 101) - 50 is integer-valued; g = mu; ghat = mu * mu. f = lambda k: ((k * k) % 101) - 50 g = mu ghat = dirichlet_inverse_pair(g, CHECK_N, mu) # for the exact integer check ghatN = dirichlet_inverse_pair(g, N, mu) # for the f = Lambda check lhs = sum(f(n - 2) * g[n] for n in range(3, CHECK_N + 1)) rhs = sum(ghat[d] * sum(f(d * m - 2) for m in range(-(-3 // d), CHECK_N // d + 1)) for d in range(1, CHECK_N + 1)) check("A. exact integer identity sum_n f(n-2)g(n) = sum_d ghat(d) sum_m f(dm-2)", lhs == rhs, f"N={CHECK_N} f(k)=((k^2)%101)-50 g=mu: lhs={lhs} rhs={rhs} equal={lhs == rhs}") # A2: the identity is not special to the shift 2 -- the same holds for shift 0, but the # inner class changes from (dm-2) to (dm): the shift is carried INTO the modulus direction. lhs0 = sum(f(n) * g[n] for n in range(2, CHECK_N + 1)) rhs0 = sum(ghat[d] * sum(f(d * m) for m in range(-(-2 // d), CHECK_N // d + 1)) for d in range(1, CHECK_N + 1)) check("A2. same exchange for shift 0 (control: identity is shift-independent in shape)", lhs0 == rhs0, f"lhs={lhs0} rhs={rhs0} equal={lhs0 == rhs0}") # ---------------------------------------------------------------- B. f = Lambda LHS = sum(lam[n - 2] * mu[n] for n in range(3, N + 1)) RHS = sum(ghatN[d] * sum(lam[d * m - 2] for m in range(1, N // d + 1)) for d in range(1, D_SMALL + 1)) RHS_full = sum(ghatN[d] * sum(lam[d * m - 2] for m in range(1, N // d + 1)) for d in range(1, N + 1)) rel = abs(RHS_full - LHS) / max(abs(LHS), 1e-30) check("B. f=Lambda: full exchange (D=N) reproduces the direct sum", rel < 1e-9, f"direct={LHS:.10f} exchange_full={RHS_full:.10f} rel_diff={rel:.2e}") # B1c. exact consistency for d = 2: d*m-2 is even, so Lambda(dm-2) != 0 only at powers of two. K2 = 0 pk = 2 while pk <= N: K2 += 1 pk *= 2 psi2 = sum(lam[2 * m - 2] for m in range(1, N // 2 + 1)) check("C. d=2 inner sum is exactly #{powers of 2 <= N} * log 2 (no primes in the even class)", abs(psi2 - K2 * math.log(2)) < 1e-9, f"psi(N;2,-2)={psi2:.10f} != {K2}*log2={K2 * math.log(2):.10f} (#2^k<=N is {K2})") # B2. where does the shift go? inner sum of exchange = psi(N; d, -2) over primes p=d*m-2. psi_shift = {} for d in range(1, D_SMALL + 1): if mu[d] == 0: continue psi_shift[d] = sum(lam[d * m - 2] for m in range(1, N // d + 1)) main = {d: (N / (d - 1)) if d > 1 else 0.0 for d in psi_shift} # heuristical main term x/phi(d) rem_small = sum(ghatN[d] * (psi_shift[d] - main.get(d, 0.0)) for d in psi_shift) rem_all = sum(ghatN[d] * (sum(lam[d * m - 2] for m in range(1, N // d + 1)) - ((N / (d - 1)) if d > 1 else 0.0)) for d in range(1, N + 1) if mu[d] != 0) print(f"\n[portrait N={N}] direct S = sum Lambda(n-2)mu(n) = {LHS:.6f}") print(f" x^(1/5) = {D_SMALL}: truncated exchange D<=x^(1/5) = {RHS:.6f} (|.|/x = {abs(RHS)/N:.3e})") print(f" remainder with the 1/phi(d) main term, D<=x^(1/5): {rem_small:.6f} ({abs(rem_small)/N:.3e} x)") print(f" remainder with the 1/phi(d) main term, D<=N : {rem_all:.6f} ({abs(rem_all)/N:.3e} x)") print(f" values of psi(N;d,-2) for the odd squarefree d<=x^(1/5): " f"{ {d: round(v, 3) for d, v in sorted(psi_shift.items())} }") out["portrait"] = {"direct_S": LHS, "exchange_D_x15": RHS, "exchange_D_full": RHS_full, "rem_small_over_x": abs(rem_small) / N, "rem_all_over_x": abs(rem_all) / N, "psi_N_d_minus2": {str(d): psi_shift[d] for d in sorted(psi_shift)}, "D_x_pow_1_5": D_SMALL} out["seconds"] = round(time.time() - T0, 2) allok = all(c["ok"] for c in out["checks"]) out["ALL_CHECKS_PASS"] = allok print(f"\nALL_CHECKS_PASS={allok} seconds={out['seconds']}") json.dump(out, open(sys.argv[2] if len(sys.argv) > 2 else "checks.json", "w"), indent=1, sort_keys=True) sys.exit(0 if allok else 1)