#!/usr/bin/env python3 """The Selberg constant for the level-n/2 square-weight sieve over F_q[x]. Results go to stdout; diagnostics go to stderr. No file is written. Dependencies: Python >= 3.8 standard library for the series; numpy only for the brute-force twin counts T(n) of the finite table (section 4.1). sympy is not used. The Selberg series. With M[d,e] = 2^omega(lcm(d,e)) q^-deg lcm(d,e) on squarefree d, e of degree <= m = n/2, the minimum of lambda^T M lambda subject to lambda_1 = 1 is 1/(M^-1)_{1,1} = 1/G(m), where (classical Selberg diagonalisation, g(P) = 2/N(P), h(P) = g/(1-g) = 2/(N(P)-2)) G(m) = sum_{d squarefree, deg d <= m} prod_{P|d} h(P) = [u^m] F(u)/(1-u), F(u) = prod_P (1 + 2 u^{deg P} / (N(P)-2)), so bound = q^n / G(m). Since F(u) = H(u)/(1-u)^2 with H(1) = 1/S(q), G(m) ~ m^2/(2 S(q)) and bound ~ 8 S(q) q^n / n^2, i.e. bound/main tends to 2^kappa kappa!: 8 for twins (kappa = 2), 2 for qrimes (kappa = 1, h(P) = 1/(N(P)-1), main term q^n/n). Floating point: binomial(I(d), j) * h^j is accumulated as a running product of exact int/int true divisions, so no huge integer is ever converted to float (the earlier version raised OverflowError at q = 5, n = 1000). """ from __future__ import annotations import math import sys def log(*a, **k): print(*a, file=sys.stderr, **k) def mobius(n: int) -> int: r, p = 1, 2 while p * p <= n: if n % p == 0: n //= p if n % p == 0: return 0 r = -r p += 1 return -r if n > 1 else r def qrime_count(q: int, d: int) -> int: """Number of monic irreducibles of degree d over F_q (Gauss).""" return sum(mobius(e) * q ** (d // e) for e in range(1, d + 1) if d % e == 0) // d def singular_series(q: int, D: int = 80) -> float: """S(q) = prod_P (1 - 2/N)/(1 - 1/N)^2 over degrees d <= D; the log of the degree-d factor is about -I(d) q^-2d, so the tail is below q^-D.""" s = 0.0 for d in range(1, D + 1): x = 1.0 / q ** d s += (qrime_count(q, d) * x) * ((math.log1p(-2 * x) - 2 * math.log1p(-x)) / x) return math.exp(s) def G(q: int, m: int, twin: bool = True) -> float: """[u^m] F(u)/(1-u), F(u) = prod_P (1 + h(P) u^deg P).""" c = [0.0] * (m + 1) c[0] = 1.0 for d in range(1, m + 1): k = qrime_count(q, d) num, den = (2, q ** d - 2) if twin else (1, q ** d - 1) new = c[:] # the j = 0 term t = [0.0] * (m // d + 1) # t[j] = binom(k, j) h^j as a float t[0] = 1.0 for j in range(1, m // d + 1): if j > k: break t[j] = t[j - 1] * ((num * (k - j + 1)) / (j * den)) for i, ci in enumerate(c): if ci == 0.0: continue for j in range(1, m // d + 1): dd = i + j * d if dd > m or t[j] == 0.0: break new[dd] += ci * t[j] c = new return sum(c) def twin_count(q: int, n: int, h: int = 2) -> int: """T(n; h) by an exact Eratosthenes sieve over monic polynomials of degree n (prime q).""" import numpy as np def monic_coeffs(d): # rows: coefficient vectors (low -> high, length d + 1, leading 1) of all monic degree-d polys idx = np.arange(q ** d, dtype=np.int64) cols = [(idx // q ** i) % q for i in range(d)] + [np.ones_like(idx)] return np.stack(cols, axis=1) if d else np.ones((1, 1), dtype=np.int64) def irreducible_mask(d): rows = monic_coeffs(d) alive = np.ones(q ** d, dtype=bool) weights = q ** np.arange(d, dtype=np.int64) for e in range(1, d // 2 + 1): for P in irr[e]: g = monic_coeffs(d - e) prod = np.zeros((g.shape[0], d + 1), dtype=np.int64) for a, pa in enumerate(P): if pa: prod[:, a:a + d - e + 1] += pa * g alive[(prod[:, :d] % q) @ weights] = False return rows, alive irr = {} for d in range(1, n // 2 + 1): rows, alive = irreducible_mask(d) irr[d] = [tuple(r) for r in rows[alive]] rows, alive = irreducible_mask(n) idx = np.arange(q ** n, dtype=np.int64) c0 = idx % q shifted = idx - c0 + (c0 + h) % q # index of f + h (h a constant) return int(np.count_nonzero(alive & alive[shifted])) def main() -> int: S = {q: singular_series(q) for q in (3, 5, 7)} log("singular series:", {q: round(v, 6) for q, v in S.items()}) print("# singular series S(q)") for q in (3, 5, 7): print(f"S({q}) = {S[q]:.6f}") print("\n# section 4.1 finite table: bound = q^n/G(n/2) = q^n/(M^-1)_{1,1}, T(n) brute force") print("q n bound T(n) bound/T(n) bound/main") try: import numpy # noqa: F401 have_np = True except ImportError: have_np = False log("numpy missing: T(n) column skipped") for q, n in ((3, 6), (3, 8), (3, 10), (3, 12), (5, 6)): b = q ** n / G(q, n // 2) main_ = S[q] * q ** n / n ** 2 if have_np: T = twin_count(q, n) print(f"{q} {n} {b:.2f} {T} {b / T:.2f} {b / main_:.3f}") else: print(f"{q} {n} {b:.2f} - - {b / main_:.3f}") print("\n# section 4.1 large-n series: bound/main = n^2/(S(q) G(n/2)), twins (limit 8)") print("q n G(m) log10(bound) bound/main") for q in (3, 5): for n in (12, 20, 40, 80, 160, 300, 600, 1000): g = G(q, n // 2) print(f"{q} {n} {g:.6g} {n * math.log10(q) - math.log10(g):.1f} {n * n / (S[q] * g):.3f}") print("\n# dimension-1 control (qrimes, q = 3): bound/main = n/G(n/2) (limit 2)") print("n G(m) bound/main") for n in (4, 12, 40, 300): g = G(3, n // 2, twin=False) print(f"{n} {g:.6g} {n / g:.3f}") return 0 if __name__ == "__main__": raise SystemExit(main())