"""Exact finite checks for Lambert sums over products of distinct primes.""" from collections import Counter from fractions import Fraction from math import comb, gcd, prod def distinct_factors(n): factors = set() divisor = 2 while divisor * divisor <= n: if n % divisor == 0: factors.add(divisor) while n % divisor == 0: n //= divisor divisor = 3 if divisor == 2 else divisor + 2 if n > 1: factors.add(n) return factors def check_divisors(limit=1000, max_factors=5): primes = [n for n in range(2, limit + 1) if distinct_factors(n) == {n}] coefficients = [[0] * (limit + 1) for _ in range(max_factors + 1)] def add_products(value, start, degree): if degree: for multiple in range(value, limit + 1, value): coefficients[degree][multiple] += 1 if degree == max_factors: return for index in range(start, len(primes)): new_value = value * primes[index] if new_value > limit: break add_products(new_value, index + 1, degree + 1) add_products(1, 0, 0) for n in range(1, limit + 1): omega = len(distinct_factors(n)) for degree in range(1, max_factors + 1): assert coefficients[degree][n] == comb(omega, degree) print(f"Squarefree-divisor coefficients checked: {limit * max_factors}") def check_blocks(): early_depths = 0 retained_projections = 0 for blocks in (1, 2): depth = 4 * blocks last = depth + depth**2 radix = 10 * (last + 1) terms = Counter({(0, 0): 1}) for block in range(blocks): scale = radix ** (2 * block) for i, width in enumerate((2, 3, radix, 1), start=1): step = width * scale position = 4 * block + i flipped = { (u + step, v + position * step): -weight for (u, v), weight in terms.items() } terms.update(flipped) terms = {point: weight for point, weight in terms.items() if weight} assert len(terms) == 12**blocks and terms[0, 0] == 1 assert len({u for u, _ in terms}) == len(terms) assert all(abs(sign) == 1 for sign in terms.values()) for k in range(1, last + 1): projected = Counter() for (u, v), sign in terms.items(): projected[k * u - v] += sign if k <= depth: assert not any(projected.values()) early_depths += 1 else: assert len(projected) == len(terms) retained_projections += len(projected) print(f"Early depths cancelled: {early_depths}") print(f"Retained projections distinct within their depth: {retained_projections}") def crt(congruences): residue, modulus = 0, 1 for target, factor in congruences: assert gcd(modulus, factor) == 1 step = (target - residue) * pow(modulus, -1, factor) % factor residue += modulus * step modulus *= factor return residue, modulus def check_balancing(): # At k=1 both scaled arguments are n+5. Each division loses one prime. rows = [(5, 0), (77, 72)] marks = {13, 17} t, _ = crt([(-1, 7), (0, 11)]) residue, modulus = crt([(0, 25), (72 + 77 * t, 77**2)] + [(-10, p) for p in sorted(marks)]) forced_primes = distinct_factors(modulus) early = retained = 0 for index in range(1, 33): n = residue + modulus * index for q, offset in rows: m, remainder = divmod(n - offset, q) assert remainder == 0 quotient_primes = distinct_factors(m + 1) assert m * q + q == n + 5 assert len(distinct_factors(n + 5)) - len(quotient_primes) == 1 early += 1 for k in range(2, 5): factors = distinct_factors(m + k) prescribed = marks if (q, k) == (5, 2) else set() assert factors & forced_primes == prescribed y, t_count = len(factors - forced_primes), len(prescribed) assert comb(len(factors), 2) == comb(t_count, 2) + t_count*y + comb(y, 2) retained += 1 # A repeated prime in a dilation still changes omega by at most one. padding = [7, 11, 13, 17] q = prod(padding) * 7 t, radical = crt([(-k, p) for k, p in enumerate(padding, start=1)]) for index in range(3): for k in range(1, 7): value = t + index * radical + k overlap = distinct_factors(value) & set(padding) expected = {padding[k - 1]} if k <= 4 else set() assert overlap == expected assert len(distinct_factors(q * value)) - len(distinct_factors(value)) == 4 - len(expected) print(f"Balanced early arguments: {early}. Retained marked or unmarked arguments: {retained}") print("Repeated-prime dilation tested at six depths on three progression elements.") def check_moments(): primes = (2, 3, 5, 7, 11) period = prod(primes) # An actual progression, with step coprime to the primes being counted. samples = Counter(sum((13 * j + 10 + 7) % p == 0 for p in primes) for j in range(period)) law = {count: Fraction(freq, period) for count, freq in samples.items()} model = {0: Fraction(1)} for p in primes: updated = Counter() for count, mass in model.items(): updated[count] += mass * Fraction(p - 1, p) updated[count + 1] += mass * Fraction(1, p) model = updated assert model == law for j in range(1, 5): observed = sum(Fraction(count**j * freq, period) for count, freq in samples.items()) assert observed == sum(value**j * mass for value, mass in model.items()) differences = Counter() for v, mass in law.items(): for w, other_mass in law.items(): differences[v - w] += mass * other_mass prime_variances = [2 * Fraction(p - 1, p**2) for p in primes] variance = sum(prime_variances) fourth = 3*variance**2 + sum(v - 3*v**2 for v in prime_variances) assert sum(x**2 * mass for x, mass in differences.items()) == variance assert sum(x**4 * mass for x, mass in differences.items()) == fourth print(f"CRT and Bernoulli laws agree on {period} residues. Difference moments of orders 2 and 4 agree.") if __name__ == "__main__": check_divisors() check_blocks() check_balancing() check_moments()