import itertools import json import math from fractions import Fraction as F def chi(t): if t <= 0: return F(1) if t >= 1: return F(0) return 1 - 10 * t**3 + 15 * t**4 - 6 * t**5 def second(t): if t <= 0 or t >= 1: return F(0) return -60 * t + 180 * t**2 - 120 * t**3 def difference(function, y, steps): total = F(0) for mask in range(1 << len(steps)): total += (-1)**mask.bit_count() * function( y + sum((h for j, h in enumerate(steps) if mask >> j & 1), F(0)) ) return total def envelope(mask, ell, modulus): selected = [ell[i] for i in range(len(ell)) if mask >> i & 1] if len(selected) <= 2: factor = math.prod(selected) else: factor = selected[0] * selected[1] * modulus(selected[2]) return 2**len(selected) * factor counts = { "profile_and_join": 0, "second_derivative_lipschitz": 0, "third_difference": 0, "full_pairing": 0, "prime_power": 0, "growth": 0, "euler_grouping": 0, "euler_moment": 0, "markov": 0, "corrected_b1_density": 0, "negative_controls": 0, } assert 30 * (F(1, 3) - F(1, 2) + F(1, 5)) == 1 assert chi(F(0)) == 1 and chi(F(1)) == 0 assert second(F(0)) == second(F(1)) == 0 assert -60 != 0 assert F(3, 16) + F(1, 2) == F(11, 16) < 1 counts["profile_and_join"] = 5 grid = [F(i, 8) for i in range(-4, 13)] for s, t in itertools.product(grid, repeat=2): assert abs(second(s) - second(t)) <= 60 * abs(s - t) counts["second_derivative_lipschitz"] += 1 for y in grid: for steps in ((F(1, 100),) * 3, (F(1, 10), F(1, 5), F(3, 10)), (F(1, 4), F(3, 4), F(5, 4))): assert abs(difference(chi, y, steps)) <= 60 * math.prod(steps) counts["third_difference"] += 1 steps = [F(1, 10), F(1, 5), F(3, 10), F(2, 5), F(3, 5), F(4, 5)] for r in range(1, 7): h = steps[:r] k = min(3, r) for y in (F(-3, 4), F(-1, 4), F(0)): full = difference(chi, y, h) paired = difference(lambda z: difference(chi, z, h[:k]), y, h[k:]) assert full == paired counts["full_pairing"] += 1 powers = [1 + i % 3 for i in range(r)] expanded = F(0) for exponents in itertools.product(*(range(a + 1) for a in powers)): if any(a > 1 for a in exponents): continue expanded += (-1)**sum(exponents) * chi( y + sum((a * v for a, v in zip(exponents, h)), F(0)) ) assert expanded == full counts["prime_power"] += 1 moduli = (lambda t: F(1), lambda t: t, lambda t: t*t, lambda t: min(F(1), 3*t), lambda t: F(0)) ell = [min(F(1), t) for t in (F(1, 10), F(1, 5), F(2, 5), F(3, 5), F(99, 100), F(3, 2))] for modulus in moduli: values = [envelope(mask, ell, modulus) for mask in range(64)] for a, b in itertools.product(range(64), repeat=2): assert values[a | b] <= 2**a.bit_count() * values[b] counts["growth"] += 1 assert envelope(7, ell, lambda t: F(2)) > 2 * envelope(3, ell, lambda t: F(2)) counts["negative_controls"] += 1 b, a = (1 << 0) | (1 << 1) | (1 << 4), 1 << 2 assert envelope(a | b, ell, lambda t: 1-t) > 2 * envelope(b, ell, lambda t: 1-t) counts["negative_controls"] += 1 assert envelope(15, ell, lambda t: t) > ( envelope(7, ell, lambda t: t) * envelope(8, ell, lambda t: t) ) counts["negative_controls"] += 1 linear = lambda t: max(F(0), min(F(1), 1-t)) h = F(1, 100) assert abs(difference(linear, -h/2, (h, h, h))) > 60*h**3 counts["negative_controls"] += 1 primes = [3, 5, 7, 11, 13, 17, 19] proxy = [F(p, 20) for p in primes] for local in ([F(2, p) for p in primes], [F(2*(p-1), p*p) for p in primes]): for modulus in moduli: direct, small = F(0), F(0) for mask in range(1 << len(primes)): indices = [i for i in range(len(primes)) if mask >> i & 1] factors = [proxy[i] for i in indices] factor = (math.prod(factors) if len(indices) < 3 else factors[0] * factors[1] * modulus(factors[2])) value = math.prod(local[i] for i in indices) * factor direct += value if len(indices) < 3: small += value grouped = small for i, j, k in itertools.combinations(range(len(primes)), 3): grouped += (local[i] * local[j] * local[k] * proxy[i] * proxy[j] * modulus(proxy[k]) * math.prod(1+t for t in local[k+1:])) assert grouped == direct counts["euler_grouping"] += 1 for start in range(len(primes) + 1): weights = local[start:] costs = primes[start:] total = math.prod((1+t for t in weights), start=F(1)) atoms = [] for mask in range(1 << len(weights)): mass = math.prod(t for j, t in enumerate(weights) if mask >> j & 1) cost = sum(t for j, t in enumerate(costs) if mask >> j & 1) atoms.append((mask, mass, cost)) assert sum(mass for _, mass, _ in atoms) == total for j, t in enumerate(weights): assert sum(mass for mask, mass, _ in atoms if mask >> j & 1) == total*t/(1+t) counts["euler_moment"] += 1 mean = sum(mass*cost for _, mass, cost in atoms) / total assert mean == sum(F(cost)*t/(1+t) for cost, t in zip(costs, weights)) counts["euler_moment"] += 1 for threshold in (F(5), F(20), F(sum(costs) + 1)): kept = sum(mass for _, mass, cost in atoms if cost <= threshold) / total assert kept >= 1 - mean/threshold counts["markov"] += 1 for mask in range(8): factors = [p for j, p in enumerate((3, 5, 7)) if mask >> j & 1] a = math.prod(factors) count = sum( 1 for n in range(a*a) if n % a == 0 and math.gcd(n//a, a) == 1 and math.gcd(n-2, a) == 1 ) assert count == math.prod(p-1 for p in factors) counts["corrected_b1_density"] += 1 for a in (2, 6): assert not any( n % a == 0 and math.gcd(n//a, a) == 1 and math.gcd(n-2, a) == 1 for n in range(a*a) ) counts["negative_controls"] += 1 print(json.dumps({ "status": "passed", "counts": counts, "scope": "Exact rational finite controls; no integer census or asymptotic validation", }, sort_keys=True))