"""route176/fobj.py -- the route-107 defect object F(h): exact value, log-law, sieve form. DEFINITION (served engine `outputs/job2038/def.c`, transcribed exactly) f_2(h) = 2*1[2|h], f_p(h) = (1 - nu_p(h)/p)/(1 - 2/p)^2, nu_p(h) = #{0, 2, h, h+2 mod p}, head(h) = prod_{3<=p<=h+2} f_p(h), Tail(x) = prod_{p>x}(p-1)/(p-2), F(h) = head(h) * Tail(h+2), normalised so F(6) = 5/3. The divergent `Tail` enters only through its ratio, giving the EXACT rational identity F(h) = (5/3) * head(h)/head(6) * prod_{8
= 5 the four points 0, 2, h, h+2 are distinct mod p unless p | h(h-2)(h+2), so f_p(h) = (p-1)/(p-2) if p | h, f_p(h) = (p-2)/(p-1) if p | h-2 or p | h+2, f_p(h) = 1 - 4/(p-2)^2 otherwise ("generic"), and for p > h+2 every p is generic. The prime p = 3 is special: nu_3(h) = 2 when 3 | h and 3 when 3 | h-+2, both giving f_3 = 3/2 < ... in fact f_3 = 3 when 3 | h and 3/2 when 3 | h-+2 (see `f_p`), so F is supported exactly on the multiples of 3: F(h) = 0 <=> h = 1 or 2 (mod 3), for h >= 4. Since every p > h+2 is generic, for p-i(h) >= 1, head(h) = [ (5/3)/head(6) ] * prod_{2
= 5} (1 - 1/(p-1)^2) collects the corrections at the exceptional primes dividing h exactly. Mertens' theorem turns the first product into a logarithm, giving the LOG LAW F(h) = A * log(h+2) * prod_{p | h, p >= 5} (1 - 1/(p-1)^2) * (1 + O(1/log h)) (L) with the explicit constant A = (5/3)/head(6) * prod_{p>=5} (1 - 4/(p-2)^2). (L) is what makes the large-H computation a sieve: mark every multiple of every prime p >= 5 with the factor (1 - 1/(p-1)^2), and F(h) = A*log(h+2)*(that product). The sieve is O(N log log N) instead of O(N pi(H)/log H). """ from __future__ import annotations import math from fractions import Fraction from functools import lru_cache import numpy as np __all__ = ["primes_upto", "nu", "f_p", "head", "tail_ratio", "F_engine", "F_exact_float", "A_const", "log_law", "sieve_F", "odd_prime_factors", "F_SUPPORT"] def primes_upto(n: int) -> list[int]: if n < 2: return [] s = bytearray([1]) * (n + 1) s[0] = s[1] = 0 for i in range(2, math.isqrt(n) + 1): if s[i]: s[i * i :: i] = bytearray(len(range(i * i, n + 1, i))) return [i for i in range(2, n + 1) if s[i]] @lru_cache(maxsize=8) def _spf(limit: int) -> list[int]: spf = list(range(limit + 1)) spf[1] = 0 i = 2 while i * i <= limit: if spf[i] == i: for j in range(i * i, limit + 1, i): if spf[j] == j: spf[j] = i i += 1 return spf def odd_prime_factors(n: int) -> list[int]: n = abs(n) if n == 0: raise ValueError while n % 2 == 0: n //= 2 if n == 1: return [] spf = _spf(max(64, n)) out = [] while n > 1: p = spf[n] out.append(p) while n % p == 0: n //= p return out def nu(p: int, h: int) -> int: return len({0, 2, h % p, (h + 2) % p}) def f_p(p: int, h: int) -> Fraction: if p == 2: raise ValueError("use f_2") return Fraction(p - nu(p, h), p) / Fraction(p - 2, p) ** 2 @lru_cache(maxsize=1) def _primes_head() -> tuple[int, ...]: return tuple(primes_upto(4001)) def head(h: int) -> Fraction: """head(h) = prod_{3<=p<=h+2} f_p(h); exact (needs h+2 <= 4000).""" out = Fraction(1) for p in _primes_head(): if p < 3: continue if p > h + 2: break out *= f_p(p, h) return out def tail_ratio(h: int) -> Fraction: """prod_{8
h + 2: break out *= Fraction(p - 1, p - 2) return out HEAD6 = head(6) # = 7/5 def F_engine(h: int) -> Fraction: """The served engine's F(h), exact rational. Needs 4 <= h <= 3998.""" if h <= 3: return Fraction(0) return Fraction(5, 3) * (head(h) / HEAD6) * tail_ratio(h) def F_exact_float(h: int) -> float: return float(F_engine(h)) if 4 <= h <= 3998 else float("nan") # ------------------------------------------------------------------ the log law @lru_cache(maxsize=4) def _G(limit: int = 3_000_000) -> float: """G = prod_{p>=5} (1 - 4/(p-2)^2), double precision.""" g = 1.0 for p in primes_upto(limit): if p >= 5: g *= (1.0 - 4.0 / (p - 2) ** 2) return g A_const = (5.0 / 3.0) / float(HEAD6) * _G() def log_law(h: int, A: float | None = None) -> float: """A * log(h+2) * prod_{p|h, p>=5}(1 - 1/(p-1)^2).""" if A is None: A = A_const x = A * math.log(h + 2) for p in odd_prime_factors(h): if p >= 5: x *= 1.0 - 1.0 / (p - 1) ** 2 return x def C_exact(limit: int = 3_000_000) -> float: """C = (5/3)/head(6) * prod_{p>=5, p<=limit} (1-4/(p-2)^2), the pinned constant.""" g = 1.0 for p in primes_upto(limit): if p >= 5: g *= 1.0 - 4.0 / (p - 2) ** 2 return (5.0 / 3.0) / float(HEAD6) * g # ------------------------------------------------------------------ sieve form def sieve_F(N: int) -> np.ndarray: """F(h) for 4 <= h <= N by the log law (L), as a float64 array of length N+1. V[h] = prod over odd primes p >= 5 dividing h of (1 - 1/(p-1)^2), built by sieving; F[h] = A_const * log(h+2) * V[h], and 0 when 3 does not divide h. """ V = np.ones(N + 3, dtype=np.float64) for p in primes_upto(N + 2): if p < 5: continue c = 1.0 - 1.0 / (p - 1) ** 2 V[p::p] *= c h = np.arange(N + 3, dtype=np.float64) F = A_const * np.log(h + 2.0) * V F = np.where((h % 3 == 0) & (h >= 6), F, 0.0) return F F_SUPPORT = "h >= 6 with 3 | h" if __name__ == "__main__": anchors = {6: 5 / 3, 12: 4.9518756589463644, 30: 8.9572308728959484, 48: 5.1546993122668496} for h, want in anchors.items(): got = F_exact_float(h) print(f"h={h:3d} F={got:.12f} anchor={want:.12f} rel={abs(got-want)/want:.2e}") print(f"A = {A_const:.15f}") print() print(" h F_exact F_law rel") for h in (12, 30, 48, 210, 2310, 30030, 510510, 9699690): if h <= 3998: e = F_exact_float(h) else: e = float("nan") lw = log_law(h) if e == e: print(f"{h:8d} {e:.9f} {lw:.9f} {abs(e-lw)/e:.2e}") else: print(f"{h:8d} {'-':>9s} {lw:.9f}")