"""Exact, independent checks for the mixed factorial proof at highest degree one.""" from fractions import Fraction from math import gcd, lcm, prod def factor(n): factors = {} candidate = 2 while candidate * candidate <= n: while n % candidate == 0: factors[candidate] = factors.get(candidate, 0) + 1 n //= candidate candidate += 1 if n > 1: factors[n] = 1 return factors def divisors(n): values = [1] for p, exponent in factor(n).items(): values = [d * p**j for d in values for j in range(exponent + 1)] return values def normalized_mean(k, q, a): """Mean of sigma_k(n)/n^k on n=qv+a, divided by zeta(k+1).""" return sum( Fraction(1, g**k) * prod(1 - Fraction(1, p**(k + 1)) for p in factor(q // g)) for g in divisors(gcd(q, a)) ) def crt(residues, moduli): modulus = prod(moduli) value = 0 for residue, m in zip(residues, moduli): other = modulus // m value += residue * other * pow(other, -1, m) return value % modulus def primitive_kernel(row1, row2): cross = [row1[i] * row2[j] - row1[j] * row2[i] for i, j in ((1, 2), (2, 0), (0, 1))] denominator = lcm(*(x.denominator for x in cross)) integers = [int(x * denominator) for x in cross] content = gcd(*integers) assert content > 0 return [x // content for x in integers] def check_mixed_endpoint(): # For m=4 and K=1 the construction gives L=3, R=3, D=96 and B=193. # On r=2 mod 4 choose w_1(3)=-1 and w_1(0)=1. The lower-degree # weight can have any value at 3. Both early rows must vanish. base, step = 193, 96 slopes = [base + step * e for e in range(3)] offsets = [step * e for e in range(3)] assert slopes == [193, 17**2, 5 * 7 * 11] tau = [len(divisors(p)) for p in slopes] sigma = [sum(divisors(p)) for p in slopes] early = [[Fraction(p, s) for p, s in zip(slopes, denominators)] for denominators in (tau, sigma)] weights = primitive_kernel(*early) for row in early: assert sum(c * x for c, x in zip(weights, row)) == 0 moduli = [4] + [p**2 for p in slopes] q = prod(moduli) start = q + crt([2] + offsets, moduli) for p, offset in zip(slopes, offsets): n, remainder = divmod(start - offset, p) assert remainder == 0 and n % p == 0 and n % 4 == 2 for shift in (1, 2): assert gcd(n + shift, p) == 1 assert start + shift * p - offset == p * (n + shift) assert {p - off for p, off in zip(slopes, offsets)} == {193} shifts = [2 * p - off for p, off in zip(slopes, offsets)] assert shifts == [386, 482, 578] coefficients = [Fraction(c * p**2, s) for c, p, s in zip(weights, slopes, sigma)] prime = 587 assert factor(prime) == {prime: 1} and prime > max(shifts) and q % prime def refined_mean(residue): finer_start = crt([start, residue], [q, prime]) return sum( coef * normalized_mean(1, q * prime, finer_start + shift) for coef, shift in zip(coefficients, shifts) ) baseline = refined_mean(0) for coef, shift in zip(coefficients, shifts): change = refined_mean(-shift) - baseline assert change == coef * normalized_mean(1, q, start + shift) / prime assert change != 0 print(f"K=1, m=4: primitive common kernel {weights}; isolated shifts {shifts}.") print("All three exact changes of the normalized mean are nonzero and have the predicted value.") def check_congruence_counts(): comparisons = 0 for q in range(1, 13): for a in range(1, 2 * q + 1): for d in range(1, 41): count = sum((q * v + a) % d == 0 for v in range(d)) g = gcd(q, d) assert count == (g if a % g == 0 else 0) comparisons += 1 print(f"{comparisons} complete-period counts agree, including noncoprime residues.") if __name__ == "__main__": check_mixed_endpoint() check_congruence_counts()