"""Exact finite checks for the factorial divisor-sum proof. Python standard library.""" from fractions import Fraction from itertools import product from math import factorial, isqrt, prod def prime_divisors(n): factors = [] for p in range(2, isqrt(n) + 1): if n % p: continue factors.append(p) while n % p == 0: n //= p if n == 1: break if n > 1: factors.append(n) return factors def mobius(n): primes = prime_divisors(n) if any(n % (p*p) == 0 for p in primes): return 0 return (-1)**len(primes) def collect(weights): """Sieve the coefficients of the double sum by exponent n=d*m.""" terms = [0] * len(weights) for d in range(1, len(weights)): for m in range(1, (len(weights) - 1)//d + 1): terms[d*m] += m * weights[d] return terms def check_named_functions(limit): mu = [0] + [mobius(n) for n in range(1, limit+1)] sigma = collect([0] + [1]*limit) phi = collect(mu) psi = collect([x*x for x in mu]) # Changing the sign only at 2 negates the squarefree weights of even divisors. flipped = collect([value if d % 2 else -value for d, value in enumerate(mu)]) for n in range(1, limit+1): primes = prime_divisors(n) radical = prod(primes) assert phi[n] == n//radical * prod(p-1 for p in primes) assert psi[n] == n//radical * prod(p+1 for p in primes) assert flipped[n] == (phi[n] if n % 2 else 3 * phi[n]) assert sigma[n] == sum(d for d in range(1, n+1) if n % d == 0) assert (sigma[n] == psi[n]) == (mu[n] != 0) # The infinite tail beyond the first term is bounded above by # 2 sqrt(k+1)/(k-1). Verify a rational gap from the integers. tested = 0 for p in range(67, limit//4 + 1): if prime_divisors(p) != [p]: continue k = 4*p first = Fraction(sigma[k]-psi[k], k) assert first == Fraction(p+1, 4*p) assert 256*(k+1) < (k-1)**2 # the error bound is less than 1/8 assert Fraction(1, 4) < first < first + Fraction(1, 8) < Fraction(1, 2) tested += 1 print(f"Classical divisor identities and the sign change at 2 through {limit}; {tested} certified 4p tail intervals.") def check_arbitrary_weights(limit): weights = [0] + [(-1)**(n//7) * isqrt(n) for n in range(1, limit+1)] values = collect(weights) tests = 0 for m in range(1, 76): for p in (2, 3, 5, 7, 11, 13, 17): if m % p == 0 or m*p > limit: continue increment = sum( (Fraction(weights[p*d], p*d) for d in range(1, m+1) if m % d == 0), Fraction(0), ) assert Fraction(values[m*p], m*p) - Fraction(values[m], m) == increment tests += 1 cutoff = 75 one_sum = sum(Fraction(values[n], factorial(n)) for n in range(1, cutoff+1)) double_sum = sum( Fraction(weights[d]*m, factorial(d*m)) for d in range(1, cutoff+1) for m in range(1, cutoff//d+1) ) assert one_sum == double_sum print(f"Signed-weight rearrangement through {cutoff}; {tests} exact prime-multiple identities.") def check_boundaries(limit): for M in (1, 2, 6, 11, 37, 101): weights = [0] * (limit+1) for t in range(1, limit//M+1): weights[M*t] = t * mobius(t) assert collect(weights) == [int(n == M) for n in range(limit+1)] # An admissible pair in the exact perturbation criterion. Compute the # shifted coefficients independently from the finite divisor weights. c = {2: 6, 3: -3, 7: 14} def B(n): return sum(value//d for d, value in c.items() if n % d == 0) F = collect([c.get(n, 0) for n in range(limit+1)]) perturbation = {1: -4, 5: 7} expected = B(1) for n in range(1, 91): expected += Fraction(perturbation.get(n, 0), factorial(n)) right = expected - Fraction(B(n+1), factorial(n)) left = sum( Fraction(F[j] - B(j+1) + perturbation.get(j, 0), factorial(j)) for j in range(1, n+1) ) assert left == right print("Six sharpness examples and 90 partial sums of a rational perturbation checked.") def check_prime_signs(): primes = (2, 3, 5, 7) selections = list(product((False, True), repeat=len(primes))) assignments = list(product((-1, 1), repeat=len(primes))) rows = [] for signs in assignments: chars = [] for selected in selections: d = prod(p for p, use in zip(primes, selected) if use) char = prod(s for s, use in zip(signs, selected) if use) chars.append(char) # Test the Euler factorization at n=d², where prime powers # in the numerator still give only squarefree divisor weights. n = d*d local_product = prod(p*(p+s) for p, s, use in zip(primes, signs, selected) if use) divisor_sum = 0 for choice in selections: divisor = prod(p for p, use in zip(primes, choice) if use) if n % divisor == 0: weight = prod(s for s, use in zip(signs, choice) if use) divisor_sum += (n//divisor)*weight assert divisor_sum == local_product rows.append(chars) for i, left in enumerate(rows): for j, right in enumerate(rows): assert sum(a*b for a, b in zip(left, right)) == (len(rows) if i == j else 0) print("Sixteen prime-sign assignments passed 256 Euler products and 256 character identities.") # Pair the columns for d | 30 with the columns for 7d. The sign at # 7 may differ across assignments, even if they agree at 2, 3, 5. base_columns = [i for i, selected in enumerate(selections) if not selected[-1]] lifted_columns = [i for i, selected in enumerate(selections) if selected[-1]] column_pairs = list(zip(base_columns, lifted_columns)) projections = 0 for signs, row in zip(assignments, rows): for test_signs, test_row in zip(assignments, rows): total = sum(row[lifted] * test_row[base] for base, lifted in column_pairs) expected = len(base_columns) * signs[-1] if signs[:-1] == test_signs[:-1] else 0 assert total == expected projections += 1 print(f"One auxiliary prime passed {projections} coefficient-recovery identities.") if __name__ == "__main__": check_named_functions(2400) check_arbitrary_weights(2400) check_boundaries(2400) check_prime_signs()