"""Exact finite checks for the sparse-prime argument and mixed-base corollary.""" from fractions import Fraction from itertools import product from math import lcm def prime_tail(prime, weights, residue, base): """Sum weights[k mod D] / base**k for k >= 1 and prime | residue+k.""" period = lcm(prime, len(weights)) first = (-residue) % prime or prime numerator = sum(weights[k % len(weights)] * base ** (period - k) for k in range(first, period + 1, prime)) return Fraction(numerator, base**period - 1) def check_single_flips(): cases = 0 for base in (2, 3): for period in (1, 2, 3, 4): for weights in product((-2, 0, 1), repeat=period): if not any(weights): continue bound = max(map(abs, weights)) for prime in (23, 31, 47): start = (prime + 1) // 2 h = next(k for k in range(start, start + period) if weights[k % period]) delta = prime_tail(prime, weights, -h, base) - prime_tail(prime, weights, 0, base) leading = Fraction(weights[h % period], base**h) error_bound = Fraction(2 * bound, base**prime - 1) assert abs(delta - leading) <= error_bound < abs(leading) assert delta * leading > 0 cases += 1 return cases def check_progression_averages(): weights = {2: (2, -1, 0), 5: (-1, 2, 1), 7: (1, 0, -2), 11: (-2, 0, 1)} period = 3 base = 2 full_period = lcm(period, *weights) values = {p: [prime_tail(p, row, r, base) for r in range(p)] for p, row in weights.items()} absolute_values = {p: [prime_tail(p, tuple(map(abs, weights[p])), r, base) for r in range(p)] for p in (7, 11)} full_series = sum(row[0] for row in values.values()) cases = 0 for residues in product(range(2), range(5)): target = values[2][residues[0]] + values[5][residues[1]] prescribed = [n for n in range(0, full_period, period) if (n % 2, n % 5) == residues] error_sum = cost_sum = Fraction(0) for n in prescribed: actual = sum(row[n % p] for p, row in values.items()) assert (actual - base**n * full_series).denominator == 1 error_sum += abs(actual - target) cost_sum += sum(row[n % p] for p, row in absolute_values.items()) cases += 1 expected_cost = sum( Fraction(sum(abs(weights[p][k % period]) * base**(period-k) for k in range(1, period + 1)), p * (base**period - 1)) for p in (7, 11) ) assert error_sum <= cost_sum == len(prescribed) * expected_cost return cases def check_masks(): primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29) cases = 0 for indices in ((1, 2), (2, 3, 6), (2, 4, 6, 9)): period = lcm(*indices) for column in product((-1, 0, 1), repeat=len(indices)): mask = [sum(weight for j, weight in zip(indices, column) if k % j == 0) for k in range(period)] if any(column): first = next(j for j, weight in zip(indices, column) if weight) assert mask[first % period] == column[indices.index(first)] assert bool(any(column)) == bool(any(mask)) cases += 1 weights = {(j, p): (p + j*j) % 7 - 3 for j in indices for p in primes} for n in range(1, 451): direct = sum(value for (j, p), value in weights.items() if n % (j*p) == 0) exceptional = sum(value for (j, p), value in weights.items() if period % p == 0 and n % (j*p) == 0) masked = sum(weights[j, p] for p in primes if period % p and n % p == 0 for j in indices if n % j == 0) assert direct == exceptional + masked cases += 1 return cases if __name__ == "__main__": print(f"{check_single_flips()} exact one-prime flips with their uniform error bounds.") print(f"{check_progression_averages()} exact tail-grid and progression-average checks.") print(f"{check_masks()} mixed-base coefficient and mask checks.")