"""Exact checks for mixed divisor degrees in factorial series, using the standard library.""" from fractions import Fraction as F from itertools import product from math import gcd, isqrt, prod def divisors(n): for d in range(1, isqrt(n) + 1): if n % d == 0: yield d if d * d != n: yield n // d def distinct_prime_factors(n): p = 2 while p * p <= n: if n % p == 0: yield p while n % p == 0: n //= p p += 1 if n > 1: yield n def sigma(n, degree): return sum(d**degree for d in divisors(n)) def mean_over_zeta(q, a, degree): # Group the divisor average by g = gcd(d,q); sum the coprime Euler factors. return sum( F(1, g**degree) * prod( 1 - F(1, p**(degree + 1)) for p in distinct_prime_factors(q // g) ) for g in divisors(gcd(q, a)) ) def check_mixed_endpoint(): # The weights on even and odd indices for degrees zero and one. w0, w1 = (-2, 5), (1, -1) p_list = (3, 5, 7) offsets = (0, 2, 4) u_list = (35, -63, 30) early_shifts = tuple(p - t for p, t in zip(p_list, offsets)) final_shifts = tuple(2 * p - t for p, t in zip(p_list, offsets)) assert early_shifts == (3, 3, 3) and final_shifts == (6, 8, 10) for degree in (0, 1): assert sum(F(u * p, sigma(p, degree)) for p, u in zip(p_list, u_list)) == 0 final_weights = tuple(F(u * p**2, sigma(p, 1)) for p, u in zip(p_list, u_list)) assert final_weights == (F(315, 4), F(-525, 2), F(735, 4)) q = 2 * prod(p * p for p in p_list) fresh_prime = 11 checks = 0 for parity in (0, 1): a = next(x for x in range(1, q + 1) if x % 2 == parity and all(x % (p * p) == t for p, t in zip(p_list, offsets))) for n_global in (a, a + q, a + 2 * q): early = [F(0), F(0)] mixed_two_terms = F(0) local_top = F(0) exact_remainder = F(0) for p, t, u in zip(p_list, offsets, u_list): n = (n_global - t) // p x, y = n + 1, n + 2 assert n % p == 0 and n % 2 == parity assert gcd(p, x) == gcd(p, y) == 1 and p * x == n_global + 3 for degree, w in enumerate((w0, w1)): early[degree] += F(u * w[x % 2] * sigma(x, degree), x) mixed_two_terms += u * ( F(w[x % 2] * sigma(x, degree), x) + F(w[y % 2] * sigma(y, degree), x * y) ) local_top += F(u * w1[y % 2] * sigma(y, 1), y**2) # Exact remainders of the first two tail terms after degree-wise truncation. exact_remainder += u * ( F(w0[y % 2] * sigma(y, 0), x * y) + F(w1[y % 2] * sigma(y, 1), x * y**2) ) global_top = sum( weight * w1[parity] * F(sigma(n_global + s, 1), (n_global + s)**2) for s, weight in zip(final_shifts, final_weights) ) assert early == [0, 0] assert local_top == global_top == mixed_two_terms - exact_remainder top = final_shifts[0] starts = [next(a + q * v for v in range(fresh_prime) if (a + q * v) % fresh_prime == residue % fresh_prime) for residue in (0, -top)] for q_test, a_test in [(q, a), *((q * fresh_prime, start) for start in starts)]: for s in final_shifts: for d in range(1, fresh_prime**2 + 1): count = sum((q_test * v + a_test + s) % d == 0 for v in range(d)) g = gcd(d, q_test) assert count == (g if (a_test + s) % g == 0 else 0) checks += 1 change = F(0) for s, weight in zip(final_shifts, final_weights): difference = (mean_over_zeta(q * fresh_prime, starts[1] + s, 1) - mean_over_zeta(q * fresh_prime, starts[0] + s, 1)) assert difference == (mean_over_zeta(q, a + s, 1) / fresh_prime if s == top else 0) change += weight * w1[parity] * difference assert change == (F(768, 77), F(-2304, 385))[parity] print(f"Parity {parity}, N = {a} mod {q}, signed difference of means / zeta(2) = {change}.") print(f"{checks:,} exact congruence densities; six exact two-term factorial-tail comparisons.") def check_degree_five_geometry(): # c_(i,0) is zero for i>0 because its generating series is the empty product. orders = [(h, j, i) for h, last in ((2, 2), (5, 4)) for i in range(last + 1) for j in range(i + 1) if j > 0 or i == 0] side = len(orders) + 1 assert side == 16 radix = 5 * (side - 1) + 1 place_values = tuple((5 - axis) * radix**axis for axis in reversed(range(5))) previous = -1 count = 0 for digits in product(range(side), repeat=5): shift_digit_part = sum(d * v for d, v in zip(digits, place_values)) assert shift_digit_part > previous previous = shift_digit_part count += 1 equations = len(orders) * side**4 assert count == 1048576 and equations == 983040 print(f"Degrees 2 and 5: {count:,} distinct terminal shifts; at most {equations:,} equations.") if __name__ == "__main__": check_mixed_endpoint() check_degree_five_geometry()