"""Exact finite checks for the signed Lambert-series audit. Python standard library.""" from collections import Counter from fractions import Fraction from functools import lru_cache from itertools import combinations, product from math import gcd, isqrt, prod @lru_cache(maxsize=None) def prime_powers(n): factors = [] p = 2 while p * p <= n: if n % p == 0: power = 1 while n % p == 0: n //= p power *= p factors.append((p, power)) p += 1 if n > 1: factors.append((n, n)) return tuple(factors) def crt(n, coordinate): # Direct idempotent formula, using each full prime power in the modulus. result = 0 for p, q in prime_powers(n): m = n // q result += coordinate(p) * m * pow(m, -1, q) return result % n def check_means(): patterns = (lambda p: 0, lambda p: 13, lambda p: 19 if p == 2 else 3, lambda p: (0, 5, 11, 16)[p % 4]) count = 0 for label in patterns: for m in range(1, 25): start = crt(m, label) for e in range(1, 46): g = gcd(m, e) residues = [(start + i * m) % e for i in range(e // g)] xg = crt(g, label) assert sorted(residues) == list(range(xg, e, g)) values = {label(p) for p, _ in prime_powers(g)} assert prod(g - xg + c for c in values) % g == 0 for b in (2, 3, 7): # The progression mean after multiplying by e/g. assert sum(b ** j for j in residues) * (b ** g - 1) == ( b ** xg * (b ** e - 1)) count += 1 print(f"Progression means by full period enumeration: {count}") def check_unit_counts(): subsets = [s for primes in [(2, 3, 5, 7), (3, 11, 17), (13, 17, 19)] for k in range(1, len(primes) + 1) for s in combinations(primes, k)] for primes in set(subsets): m = prod(primes) distribution = Counter(sum(k * (m // p) for k, p in zip(ks, primes)) % m for ks in product(*(range(1, p) for p in primes))) assert distribution == {v: 1 for v in range(1, m) if gcd(v, m) == 1} r = len(primes) denominator = 4 * (r + 1) * 2 ** r bad = sum(1 for v in distribution if min(v, m - v) * denominator < m) assert bad * denominator <= 2 * m assert m <= (r + 1) * len(distribution) print(f"CRT unit distributions verified exhaustively: {len(set(subsets))}") def choose_shifts(d): primes = tuple(p for p, _ in prime_powers(d)) subsets = [tuple(p for j, p in enumerate(primes) if mask & (1 << j)) for mask in range(1 << len(primes))] rad = prod(primes) h = isqrt(d) best = -1 for ks in product(*(range(1, p) for p in primes)): shifts = dict(zip(primes, ks)) if any(d - h < (d // p) * k for p, k in shifts.items()): continue residues = [sum((rad // p) * shifts[p] for p in s) % rad for s in subsets[1:]] distance = min(min(v, rad - v) for v in residues) if distance > best: chosen, best = shifts, distance assert best * 4 * (len(primes) + 1) * 2 ** len(primes) >= rad return chosen, subsets, Fraction(best, rad) def check_detectors(): cancellations = 0 bounds = 0 for d in (12, 18, 35, 64, 81, 105, 225, 315, 420, 840): ks, subsets, separation = choose_shifts(d) h = isqrt(d) t = d - h corners = [(s, {p: t - ks[p] * (d // p) for p in s}) for s in subsets] for e in range(1, 2 * d + 1): if e % d: hist = Counter() for s, coords in corners: x = crt(e, lambda p: coords.get(p, t)) hist[x] += (-1) ** len(s) assert not any(hist.values()) cancellations += 1 for e in (d, 2*d, 3*d, 5*d, 6*d, 10*d, d*d, 31*d): for s, coords in corners[1:]: x = crt(e, lambda p: coords.get(p, t)) residue = (t - sum(ks[p] * (d // p) for p in s)) % d assert x % d == residue u = e - x assert u >= d * separation - h possible = set(coords.values()) | {t} assert prod(u + c for c in possible) % e == 0 assert e <= (d + u) ** (len(ks) + 1) bounds += 1 if d in (64, 225, 420, 840): # Stress the tails: target coefficient of magnitude one, other # coefficients of magnitude e. Sum absolute errors separately too. b = 2 delta = Fraction(0) abs_errors = Fraction(0) target = Fraction(0) for j in (1, 2, 3, 4, 5, 6): e = j * d a = -1 if j == 1 else (-1) ** j * e for s, coords in corners: x = crt(e, lambda p: coords.get(p, t)) term = Fraction((-1) ** len(s) * a * b ** x, b ** e - 1) delta += term if j == 1 and not s: target = term else: abs_errors += abs(term) assert target == -Fraction(b ** (d - h), b ** d - 1) assert abs(delta - target) <= abs_errors # Report an exact power-of-two upper bound for the scaled error. scaled = abs_errors * b ** h k = 0 assert 0 < scaled < 1 while scaled * 2 ** (k+1) < 1: k += 1 print(f"d={d}: total absolute leakage < 2^(-h-{k}), h={h}") print(f"Exact mixed-difference cancellations (d,e): {cancellations}") print(f"Surviving multiple/corner residue and product checks: {bounds}") def check_extensions(): weights = {d: ((113 * d + d*d) % 23) - 11 for d in range(1, 65)} for b in (2, 3, 5, 11): neg, plus = Counter(), Counter() for d, a in weights.items(): neg[d] += -a if d % 2 else a if d % 2: neg[2*d] += 2*a plus[d] += a plus[2*d] -= 2*a def total(arr, base, sign): return sum(Fraction(a, base ** d + sign) for d, a in arr.items()) assert total(weights, -b, -1) == total(neg, b, -1) assert total(weights, b, 1) == total(plus, b, -1) norm = lambda arr: sum(Fraction(abs(a), d) for d, a in arr.items()) assert max(norm(plus), norm(neg)) <= 2 * norm(weights) partial = sum(Fraction(2 ** k, b ** (2 ** k) + 1) for k in range(9)) assert partial + Fraction(2 ** 9, b ** (2 ** 9) - 1) == Fraction(1, b-1) mu = {n: (0 if any(q != p for p, q in prime_powers(n)) else (-1) ** len(prime_powers(n))) for n in range(1, 513)} for n in mu: assert sum(mu[k] for k in range(1, n+1) if n % k == 0) == int(n == 1) print("Signed base and plus transformations, weighted bounds and telescopes: four bases") print("Möbius divisor identity: n=1,...,512") if __name__ == "__main__": check_means() check_unit_counts() check_detectors() check_extensions()