"""Finite exact checks for the progression means and the signed mixed differences.""" from fractions import Fraction from functools import cache from itertools import product from math import gcd, isqrt, lcm, prod @cache def prime_powers(n): factors = [] p = 2 while p * p <= n: power = 1 while n % p == 0: n //= p power *= p if power > 1: factors.append((p, power)) p += 1 if n > 1: factors.append((n, n)) return factors def residue(e, default, changes): value, modulus = 0, 1 for p, power in prime_powers(e): coordinate = changes.get(p, default) coefficient = (coordinate - value) * pow(modulus, -1, power) % power value += modulus * coefficient modulus *= power return value def check_means(): count = 0 patterns = [(0, {}), (29, {2: 0, 3: 2}), (37, {2: 18, 3: 9, 7: 3})] for base, (default, changes) in product((2, 3), patterns): alphabet = set(changes.values()) | {default} for e, m in product(range(1, 37), range(1, 31)): x_m = residue(m, default, changes) period = lcm(e, m) numerator = sum(base ** (n % e) for n in range(x_m, period, m)) exact_mean = Fraction(numerator, (period // m) * (base ** e - 1)) g = gcd(e, m) x_g = x_m % g assert x_g == residue(g, default, changes) expected = Fraction(g * base ** x_g, e * (base ** g - 1)) assert exact_mean == expected u = g - x_g assert prod(u + c for c in alphabet) % g == 0 count += 1 return count def corners(d, steps): t = d - isqrt(d) result = [(1, {})] for (p, _), k in zip(prime_powers(d), steps, strict=True): coordinate = t - k * (d // p) result += [(-sign, changes | {p: coordinate}) for sign, changes in result] return t, result def mixed_numerator(base, e, t, points): return sum(sign * base ** residue(e, t, changes) for sign, changes in points) def check_worked_example(): d = 35 t, points = corners(d, (1, 2)) assert t == 30 assert [sign for sign, _ in points] == [1, -1, -1, 1] distances = { e: [e - residue(e, t, changes) for _, changes in points] for e in (7, 35, 70) } assert distances == { 7: [5, 5, 1, 1], 35: [5, 12, 15, 22], 70: [40, 12, 50, 22], } for base in (2, 3): assert mixed_numerator(base, 7, t, points) == 0 return distances def check_cancellation(): count = 0 for d in range(2, 61): t, points = corners(d, [1] * len(prime_powers(d))) for e in range(1, 161): if e % d: assert mixed_numerator(2, e, t, points) == 0 count += 1 return count def check_multiples(): # The prime factors are 2,3,5,7. The steps are nonzero at their prime. d = 3360 steps = (1, 1, 2, 6) t, points = corners(d, steps) h = d - t min_gap = d for _, changes in points[1:]: assert min(changes.values()) >= 0 subset_step = sum(t - c for c in changes.values()) % d min_gap = min(min_gap, subset_step, d - subset_step) assert min_gap == 304 and min_gap > 2 * h weights = {d: -3, 2*d: 5, 3*d: -7, 4*d: 13, 5*d: -101, 7*d: 89} for base in (2, 3): delta = Fraction(0) for e, a in weights.items(): for _, changes in points[1:]: assert e - residue(e, t, changes) >= min_gap - h numerator = mixed_numerator(base, e, t, points) delta += Fraction(a * numerator, base ** e - 1) if e == d: numerator -= base ** t # Remove the isolated anchor. # Each absolute error is at most b^(-h-50) / len(weights). assert len(weights) * abs(a * numerator) * base ** (h+50) < base ** e - 1 scaled_delta = base ** h * delta assert abs(scaled_delta - weights[d]) < Fraction(1, 2) assert round(scaled_delta) == weights[d] return d, h, min_gap def check_mobius(): for n in range(1, 301): divisor_sum = 0 for d in range(1, n + 1): if n % d == 0: factors = prime_powers(d) if all(p == power for p, power in factors): divisor_sum += (-1) ** len(factors) assert divisor_sum == int(n == 1) if __name__ == "__main__": for exponent, distances in check_worked_example().items(): print(f"Worked example at d=35, exponent {exponent}: distances {distances}") print(f"{check_means()} exact progression means and product divisibilities passed") print(f"{check_cancellation()} exact nonmultiple cancellations passed") d, h, gap = check_multiples() print(f"d={d}, h={h}, subset distance at least {gap}/{d}") print("six signed active multiples, total error < b^(-h-50) for b=2 and b=3") print("Nearest integer to b^h times the mixed difference recovers a_3360 = -3 in both bases") check_mobius() print("Möbius negative control passed through n=300")