#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Route 162 rescue, step 2: verify the truncated bracket pair main term by EXHAUSTIVE count over a full period mod P(z). No local-density formula is trusted here: every number is counted. theta(n) = 1 iff gcd(n, P(z)) = 1. U_r(n) = sum_{d | gcd(n,P), Omega(d) <= r, d <= z^3} mu(d). With both brackets equal (U = L = U_r) the consumer identity collapses to cc = U_r(r) U_r(r+2), and the exact period means P * (pair value) = sum_{r mod P} cc(r) with no error term at all. Compare: * the true rough-pair density D(z) = #{r : gcd(r,P) = gcd(r+2,P) = 1} / P * the truncated bracket main term T(z,r) = sum_{r mod P} U_r(r) U_r(r+2) / P for r = 1..5, separately for ODD r (a lower bracket in Bonferroni's sense: U_r <= theta) and EVEN r (an upper bracket: U_r >= theta). """ from fractions import Fraction from math import gcd import itertools def primes_lt(z): out = [] for n in range(2, z): if all(n % q for q in out if q * q <= n): out.append(n) return out def mu(n): m, cnt = n, 0 for p in primes_lt(n + 1): if m % p == 0: m //= p if m % p == 0: return 0 cnt += 1 return -1 if cnt % 2 else 1 def U_table(z, r, P): """U_r(n) for n in [0, P) via d | gcd(n,P), Omega(d) <= r, d <= z^3""" ps = primes_lt(z) ds = [] for k in range(0, r + 1): for combo in itertools.combinations(ps, k): d = 1 for p in combo: d *= p if d <= z ** 3: ds.append((d, mu(d))) return [sum(a for d, a in ds if n % d == 0) for n in range(P)] print(f"{'z':>4} {'r':>2} {'kind':>5} {'T = period-avg of U_r(r)U_r(r+2)':>34} {'true D(z)':>12} {'T>0':>5} {'T>=D':>5}") for z in (5, 7, 11, 13): ps = primes_lt(z) P = 1 for p in ps: P *= p # true rough-pair density over the period true = Fraction(sum(1 for n in range(P) if gcd(n, P) == 1 and gcd(n + 2, P) == 1), P) for r in (1, 2, 3, 4, 5): Uv = U_table(z, r, P) T = Fraction(sum(Uv[n] * Uv[(n + 2) % P] for n in range(P)), P) kind = "even" if r % 2 == 0 else "odd" print(f"{z:>4} {r:>2} {kind:>5} {str(T):>34} {str(true):>12} {str(T > 0):>5} {str(T >= true):>5}") print() # cross-check the true density against its product form, both computed independently for z in (5, 7, 11, 13, 17, 19, 23): ps = [p for p in primes_lt(z) if p != 2] P = 1 for p in primes_lt(z): P *= p true = Fraction(sum(1 for n in range(P) if gcd(n, P) == 1 and gcd(n + 2, P) == 1), P) prod = Fraction(1, 2) for p in ps: prod *= Fraction(p - 2, p) n2 = [n for n in range(1, 2 * P)[:0]] print(f"z={z:>3} counted {str(true):>14} = {float(true):.8f} | product {str(prod):>14} = {float(prod):.8f} | equal {true == prod}")