"""Exact finite checks for the grid and its two uses of congruences.""" from collections import defaultdict from fractions import Fraction from itertools import product from math import comb, gcd, isqrt, prod def check_fifth_degree(): degree = 5 radix = degree + 2 powers = [radix**a for a in range(degree)] row_moments = [defaultdict(lambda: [0] * (degree + 1)) for _ in powers] distinguished = [] vertices = 0 for digits in product(range(radix), repeat=degree): vertices += 1 index = sum(x * power for x, power in zip(digits, powers)) offset = sum((a + 1) * x * powers[a] for a, x in enumerate(digits)) weight = prod((-1)**x * comb(degree + 1, x) for x in digits) moments = [weight * index**h for h in range(degree + 1)] for j, groups in enumerate(row_moments): # Within row j the integer below determines the entire shift. totals = groups[(j + 1) * index - offset] for h, moment in enumerate(moments): totals[h] += moment if (degree + 1) * index == offset: distinguished.append(digits) assert distinguished == [(0,) * degree] assert all(len(row) == radix**(degree - 1) for row in row_moments) assert all(all(value == 0 for value in totals) for row in row_moments for totals in row.values()) count = sum(len(row) for row in row_moments) * (degree + 1) print(f"Degree five: {vertices:,} vertices, {count:,} vanishing moments," f" {len(distinguished)} distinguished shift.") def sigma_one(n): result = 0 for d in range(1, isqrt(n) + 1): if n % d == 0: result += d if d * d != n: result += n // d return result def check_period_three(): dilations = (13, 19, 25) offsets = (0, 6, 12) weights = (1, -2, 1) assert [p - t for p, t in zip(dilations, offsets)] == [13] * 3 assert sum(w * p for w, p in zip(weights, dilations)) == 0 assert [2 * p - t for p, t in zip(dilations, offsets)] == [26, 32, 38] checks = 0 for r in range(3): # Begin with N = r mod 3 and add each squared dilation by CRT. n, step = r, 3 for p, t in zip(dilations, offsets): modulus = p * p n += step * (((t - n) * pow(step, -1, modulus)) % modulus) step *= modulus for p, t in zip(dilations, offsets): start, rem = divmod(n - t, p) assert rem == 0 and start % p == 0 and start % 3 == r for j in range(2): x = start + j + 1 y = n + (j + 1) * p - t assert p * x == y and gcd(x, p) == 1 assert sigma_one(p) * sigma_one(x) == sigma_one(y) checks += 1 print(f"Period three: {checks} exact divisor-sum rescalings across all residues.") def check_progression_densities(): tested = 0 for q in (1, 4, 6, 10): for a in range(1, q + 1): for d in range(1, 41): g = gcd(d, q) period = d // g count = sum((q * v + a) % d == 0 for v in range(period)) assert Fraction(count, period) == (Fraction(g, d) if a % g == 0 else 0) tested += 1 print(f"Progression means: {tested} exact divisibility densities.") if __name__ == "__main__": check_fifth_degree() check_period_three() check_progression_densities()