"""Independent exact checks for the 1 December 2026 periodic-factorial-sums audit. Run with Python 3.8 or later. Only the standard library is used. """ from fractions import Fraction from functools import lru_cache from math import comb, gcd, prod def check_expansion(): # Test the Stirling recurrence against the denominator polynomial itself. rows = [[1]] for n in range(1, 11): old = rows[-1] + [0] rows.append([0] + [j * old[j] + old[j - 1] for j in range(1, n + 1)]) count = 0 for k in range(11): for j in range(k + 1): denominator = [1] for h in range(1, j + 1): denominator = [a - h * b for a, b in zip(denominator + [0], [0] + denominator)] inverse = [rows[i][j] for i in range(j, k + 1)] for n in range(len(inverse)): coefficient = sum(denominator[s] * inverse[n - s] for s in range(min(n, j) + 1)) assert coefficient == int(n == 0) count += 1 print(f"Reciprocal-factorial expansion, degrees 0 through 10: {count} coefficients.") def check_degree_five(): k = 5 base = k + 2 one_dimensional_weights = [(-1)**digit * comb(k + 1, digit) for digit in range(base)] groups = [{} for _ in range(k)] final_zero = [] for index in range(base**k): # Read the vertex from its integer index, least significant digit first. # Coordinate a contributes digit * base**a to I. remainder = index place_values = [] weight = 1 for a in range(k): remainder, digit = divmod(remainder, base) place_values.append(digit * base**a) weight *= one_dimensional_weights[digit] moments = [weight * comb(index, h) for h in range(k + 1)] for j, groups_in_row in enumerate(groups): shift_coordinate = sum((j - a) * v for a, v in enumerate(place_values)) if shift_coordinate not in groups_in_row: groups_in_row[shift_coordinate] = [0] * (k + 1) values = groups_in_row[shift_coordinate] for h, value in enumerate(moments): values[h] += value if sum((k - a) * v for a, v in enumerate(place_values)) == 0: final_zero.append(index) assert final_zero == [0] assert all(len(row) == base**(k - 1) for row in groups) assert all(all(v == 0 for v in totals) for row in groups for totals in row.values()) count = sum(len(row) for row in groups) * (k + 1) print(f"Degree five: {base**k:,} vertices, {count:,} zero binomial moments, " f"unique final minimum at vertex {final_zero[0]}.") def crt(moduli, residues): modulus = prod(moduli) result = 0 for m, a in zip(moduli, residues): part = modulus // m result += a * part * pow(part, -1, m) return result % modulus def sigma_one(n): result = 1 factor = 2 while factor**2 <= n: factor_sum = 1 while n % factor == 0: n //= factor factor_sum = 1 + factor * factor_sum result *= factor_sum factor += 1 if factor == 2 else 2 return result * (1 + n if n > 1 else 1) @lru_cache(maxsize=None) def hits(step, offset, d): # Count directly on a complete d-period, even when a shorter period exists. return sum((step * v + offset) % d == 0 for v in range(d)) def truncated_mean(q, a, k, cutoff): return sum((Fraction(hits(q % d, a % d, d), d**(k + 1)) for d in range(1, cutoff + 1)), Fraction()) def check_densities_and_refinements(): count = 0 for q in range(1, 13): for a in range(1, q + 1): for d in range(1, 65): g = gcd(d, q) assert hits(q % d, a % d, d) == (g if a % g == 0 else 0) count += 1 print(f"Divisibility densities: {count:,} full-period counts.") pairs = [(1, 1, 2), (4, 4, 3), (6, 4, 5), (9, 6, 2), (10, 7, 3), (12, 8, 5)] count = 0 for q, a, prime in pairs: divisible = crt((q, prime), (a, 0)) + q * prime indivisible = crt((q, prime), (a, 1)) + q * prime for k in range(1, 5): for cutoff in (35, 63, 128): old = truncated_mean(q, a, k, cutoff) shorter = truncated_mean(q, a, k, cutoff // prime) base = old - shorter / prime**(k + 1) assert truncated_mean(q * prime, indivisible, k, cutoff) == base assert truncated_mean(q * prime, divisible, k, cutoff) == base + shorter / prime**k count += 2 print(f"Fresh-prime refinement with a finite divisor cutoff: {count} exact identities.") def check_period_three(): moduli = (3, 13**2, 19**2, 25**2) step = prod(moduli) dilations = (13, 19, 25) offsets = (0, 6, 12) signs = (1, -2, 1) weight = (1, -2, 0) prime = 41 cutoff = 192 assert [z * sigma_one(p) for z, p in zip(signs, dilations)] == [14, -40, 31] assert sum(z * p for z, p in zip(signs, dilations)) == 0 assert [p - t for p, t in zip(dilations, offsets)] == [13] * 3 shifts = [2 * p - t for p, t in zip(dilations, offsets)] assert shifts == [26, 32, 38] differences = [] count = 0 for r in range(3): a = crt(moduli, (r, *offsets)) + step for p, t in zip(dilations, offsets): n, rem = divmod(a - t, p) assert rem == 0 and n % p == 0 and n % 3 == r for depth in (1, 2): x = n + depth y = a + p * depth - t assert y == p * x and gcd(p, x) == 1 assert sigma_one(p) * sigma_one(x) == sigma_one(y) count += 1 mark = weight[(r + 2) % 3] coefficients = [z * p**2 * mark for z, p in zip(signs, dilations)] miss = crt((step, prime), (a, 0)) + step * prime hit = crt((step, prime), (a, -shifts[0])) + step * prime difference = sum(c * (truncated_mean(step * prime, hit + s, 1, cutoff) - truncated_mean(step * prime, miss + s, 1, cutoff)) for c, s in zip(coefficients, shifts)) prediction = Fraction(coefficients[0], prime) * truncated_mean( step, a + shifts[0], 1, cutoff // prime) assert difference == prediction differences.append((difference > 0) - (difference < 0)) assert differences == [0, 1, -1] print(f"Period-three example: {count} multiplicative rescalings; " "signed mean differences have signs 0, +, - as predicted.") if __name__ == "__main__": check_expansion() check_degree_five() check_densities_and_refinements() check_period_three()