"""Exact arithmetic checks for the November 26 prime Lambert sums audit. Standard library only.""" from collections import Counter from fractions import Fraction from itertools import combinations from math import gcd, isqrt, lcm, prod def interpolation(): # Newton's backward-difference expansion at the endpoint z=1, step 1/102. # At the endpoint the derivative of the order-j basis is 102/j. for k in range(102): values = [(102 - j) ** k for j in range(102)] derivative = Fraction() for j in range(1, 102): values = [a - b for a, b in zip(values, values[1:])] derivative += Fraction(102 * values[0], j) assert derivative == k * 102**k print("All 102 derivative identities hold over the rationals.") def weighted_period(values): period = len(values) return Fraction(sum(v << (period - j) for j, v in enumerate(values, 1)), 2**period - 1) def dilation(): weights = {2: 2, 3: -3, 5: 1} period = prod(weights) def digit(m): return sum(w for p, w in weights.items() if m % p == 0) for u in range(1, period + 1): args = range(u + 1, u + period + 1) ordinary = weighted_period([digit(m) for m in args]) for p, w in weights.items(): dilated = weighted_period([digit(p * m) for m in args]) correction = weighted_period([int((p * m) % (p * p) == 0) for m in args]) assert dilated == ordinary + w * (1 - correction) print("All 90 infinite periodic tail identities give the stated sign of the p² correction.") def prime_factors(n): result = set() for p in range(2, isqrt(n) + 1): if n % p == 0: result.add(p) while n % p == 0: n //= p if n > 1: result.add(n) return result def cube(base, steps, height): k = len(steps) vertices = [] terms = [] layers = [Counter() for _ in steps] for mask in range(2**k): used = [j for j in range(k) if mask & (1 << j)] prime = base + sum(steps[j] for j in used) offset = sum((j + 1) * steps[j] for j in used) sign = (-1) ** len(used) vertices.append((prime, offset)) for i, layer in enumerate(layers, 1): layer[i * prime - offset] += sign for h in range(1, height + 1): terms.append(((k + h) * prime - offset, sign * 2 ** (height - h))) assert all(all(value == 0 for value in layer.values()) for layer in layers) assert len(set(s for s, _ in terms)) == len(terms) return vertices, terms, 2 ** (k + height) def progression(vertices, terms): prescribed = dict(vertices) primes = set(prescribed) for (s, _), (t, _) in combinations(terms, 2): primes.update(prime_factors(abs(s - t))) mod = prod(primes) # CRT projectors reconstruct the representative using only its defining conditions. first = sum(b * (mod // p) * pow(mod // p, -1, p) for p, b in vertices) % mod assert all(first % p == prescribed.get(p, 0) % p for p in primes) return first, mod def local_integer(n, prime, weight, terms): return weight * sum(coefficient for s, coefficient in terms if (n + s) % prime == 0) def residue_tests(): vertices, terms, den = cube(5, [2], 2) first, step = progression(vertices, terms) assert (first, step) == (30, 210) pairs = [(local_integer(n, 11, -3, terms), local_integer(n, 13, 3, terms)) for n in range(first, first + step * 143, step)] marginal_x = [local_integer(n, 11, -3, terms) for n in range(11)] marginal_y = [local_integer(n, 13, 3, terms) for n in range(13)] assert Counter(pairs) == Counter((x, y) for x in marginal_x for y in marginal_y) assert Fraction(sum(x*x for x in marginal_x), 11 * den**2) == Fraction(45, 352) assert Fraction(sum(y*y for y in marginal_y), 13 * den**2) == Fraction(45, 416) for a in range(5): for b in range(5): assert sum(x**a * y**b for x, y in pairs) == ( sum(x**a for x in marginal_x) * sum(y**b for y in marginal_y)) print("The complete joint histogram factors on 143 points. All 25 mixed moments agree.") vertices, terms, den = cube(11, [2, 6], 2) first, step = progression(vertices, terms) assert (first, step) == (953736630, 6469693230) for p, b in vertices: for k in range(1, 21): residues = [((first + j * step - b) // p + k) % p for j in range(p)] assert sorted(residues) == list(range(p)) assert sum((first + j * step - b + k*p) % (p*p) == 0 for j in range(p)) == 1 count = 37 * 43 totals = [local_integer(n, 37, 1, terms) + local_integer(n, 43, -1, terms) for n in range(first, first + step * count, step)] second = Fraction(sum(x*x for x in totals), count * den**2) assert not sum(totals) and second == Fraction(25, 6364) print(f"Both cubes cancel the early shifts. Four-vertex CRT: {first} modulo {step}.") print(f"All 80 square counts agree. Signed second moment on {count} points: {second}.") def rough_divisors(): limit, threshold = 2500, 13 factors = [set()] + [prime_factors(m) for m in range(1, limit + 1)] for a in (1, 2): eligible = {p for p in range(threshold + 1, limit + 1) if factors[p] == {p} and p % 3 == a} for z in (Fraction(1, 2), Fraction(2, 3)): expanded = [Fraction() for _ in range(limit + 1)] for ell in range(1, limit + 1): divisors = factors[ell] if divisors <= eligible and prod(divisors) == ell: coefficient = (z - 1) ** len(divisors) for m in range(ell, limit + 1, ell): expanded[m] += coefficient assert all(expanded[m] == z ** len(factors[m] & eligible) for m in range(1, limit + 1)) print("The squarefree-divisor expansion equals the class-count test in all 10,000 cases.") def refinement(): cases = [ ([(1, 4, 1), (1, 8, -1), (5, 8, -1)], 0), ([(2, 3, 1), (5, 6, -1)], Fraction(1, 3)), ([(1, 2, 1), (1, 6, -1), (5, 6, -1)], Fraction(1, 7)), ([(1, 2, 1), (1, 10, -1), (3, 10, -1), (7, 10, -1), (9, 10, -1)], Fraction(1, 31)), ] for terms, expected in cases: modulus = lcm(*(d for _, d, _ in terms)) weights = [sum(c for a, d, c in terms if r % d == a) for r in range(modulus)] assert all(w == 0 for r, w in enumerate(weights) if gcd(r, modulus) == 1) assert sum(Fraction(weights[p], 2**p - 1) for p in prime_factors(modulus)) == expected print("Four modulus refinements give exact rational corrections 0, 1/3, 1/7 and 1/31.") if __name__ == "__main__": interpolation() dilation() residue_tests() rough_divisors() refinement()