"""Independent exact spot checks for the December 3 factorial-totient paper.""" from fractions import Fraction from functools import cache from itertools import product from math import factorial, isqrt, prod LIMIT = 5040 @cache def divisors(n): small = [d for d in range(1, isqrt(n) + 1) if n % d == 0] return small + [n // d for d in reversed(small) if d * d != n] @cache def primes_of(n): return [p for p in divisors(n) if p > 1 and len(divisors(p)) == 2] @cache def mu(n): if any(n % (p * p) == 0 for p in primes_of(n)): return 0 return (-1) ** len(primes_of(n)) def local_value(n, prime_parameter): primes = primes_of(n) return (n // prod(primes)) * prod(p + prime_parameter(p) for p in primes) def numerator(n, weight): return sum(n // d * weight(d) for d in divisors(n)) def check_totients(): parameters = (-7, -3, -1, 0, 1, 2, 6) for t in parameters: weights = [0] + [t ** len(primes_of(d)) if mu(d) else 0 for d in range(1, LIMIT + 1)] for n in range(1, LIMIT + 1): assert local_value(n, lambda p: t) == numerator(n, weights.__getitem__) for n in range(1, LIMIT + 1): phi = local_value(n, lambda p: -1) flip_at_two = local_value(n, lambda p: 1 if p == 2 else -1) assert flip_at_two == (3 if n % 2 == 0 else 1) * phi print(f"Euler products and divisor sums agree for {len(parameters)} integer " f"parameters at all n from 1 to {LIMIT}. The parity example agrees throughout.") def discrepancy(n): return sum(divisors(n)) - local_value(n, lambda p: 1) def check_tails(): count = 0 for p in range(3, (LIMIT - 5) // 4 + 1): if len(divisors(p)) != 2: continue k = 4 * p assert Fraction(discrepancy(k), k) == Fraction(p + 1, 4 * p) denominator = 1 lower = Fraction(0) for n in range(k, k + 6): denominator *= n lower += Fraction(discrepancy(n), denominator) # For n >= k+6, 0 <= sigma(n)-psi(n) <= n(n-1). The remaining # factorial tail is bounded by twice its first majorizing term. upper = lower + Fraction(2, prod(range(k, k + 5))) assert Fraction(1, 4) < lower <= upper < Fraction(1, 2) count += 1 print(f"Exact six-term enclosures put {count} normalized tails at k=4p in (1/4,1/2).") def check_general_weights(): def weight(d): return (d % 5 - 2) * isqrt(d) values = [0] + [numerator(n, weight) for n in range(1, LIMIT + 1)] for n in range(1, LIMIT + 1): recovered = sum((n // d) * mu(n // d) * values[d] for d in divisors(n)) assert recovered == weight(n) count = 0 for m in range(1, 74): for p in (3, 11, 23, 43, 67): if m % p: difference = Fraction(values[m*p], m*p) - Fraction(values[m], m) assert difference == sum(Fraction(weight(p*d), p*d) for d in divisors(m)) count += 1 cutoff = 90 from_n = sum(Fraction(values[n], factorial(n)) for n in range(1, cutoff + 1)) from_d = sum(Fraction(j * weight(d), factorial(j*d)) for d in range(1, cutoff + 1) for j in range(1, cutoff // d + 1)) assert from_n == from_d print(f"Recovered every signed weight through {LIMIT} by inversion. Verified " f"{count} prime-multiple differences and the kernel rearrangement through {cutoff}.") def check_perturbation(): weights = {1: -2, 4: 12, 7: -14, 10: 10} defects = {2: 3, 5: -12} def periodic(n): return -2 + 3 * (n % 4 == 0) - 2 * (n % 7 == 0) + (n % 10 == 0) running = Fraction(0) predicted = Fraction(periodic(1)) for n in range(1, 141): f = numerator(n, lambda d: weights.get(d, 0)) assert f == n * periodic(n) h = -periodic(n + 1) + defects.get(n, 0) running += Fraction(f + h, factorial(n)) predicted += Fraction(defects.get(n, 0), factorial(n)) assert running + Fraction(periodic(n + 1), factorial(n)) == predicted assert predicted == Fraction(-3, 5) for m in (1, 4, 10, 31, 48, 103): def spike_weight(d): return (d // m) * mu(d // m) if d % m == 0 else 0 for n in range(1, LIMIT + 1): assert numerator(n, spike_weight) == int(n == m) print(f"Checked 140 partial sums for a perturbation with limit -3/5 and " f"six single-term numerators at linear weight growth through {LIMIT}.") def check_sign_recovery(): primes = (2, 3, 5, 7, 11) q = 13 assignments = list(product((-1, 1), repeat=len(primes))) indices = divisors(prod(primes)) count = 0 for signs in assignments: local_sign = dict(zip(primes, signs)) for outer_sign in (-1, 1): local_sign[q] = outer_sign lifted = [prod(local_sign[p] for p in primes_of(q*d)) for d in indices] for probe in assignments: dots = [prod(s for p, s in zip(primes, probe) if d % p == 0) for d in indices] coefficient = sum(a * b for a, b in zip(lifted, dots)) assert coefficient == (len(indices) * outer_sign if signs == probe else 0) count += 1 print(f"Verified {count} character projections for all five-prime restrictions " "and both signs at the auxiliary prime 13.") if __name__ == "__main__": check_totients() check_tails() check_general_weights() check_perturbation() check_sign_recovery()