"""Reproduce the short-list, conductor-compression and discriminant examples. Run with Python 3.8 or later. Uses only the standard library. The interval table is an exact count of primes; its comparison column is count/r^k. """ from collections import Counter from functools import lru_cache from itertools import combinations, product from math import gcd, isqrt def primes_upto(limit): flags = bytearray(b"\1") * (limit + 1) flags[:2] = b"\0\0" for q in range(2, isqrt(limit) + 1): if flags[q]: flags[q*q::q] = b"\0" * (1 + (limit - q*q)//q) return [i for i in range(2, limit + 1) if flags[i]] @lru_cache(maxsize=None) def frobenius_histogram(n, p): """Return pairs (degree, number of factors) of the complete Phi_n in F_p.""" assert gcd(n,p) == 1 unvisited = {a for a in range(n) if gcd(a,n) == 1} counts = Counter() while unvisited: start = min(unvisited) i, size = start, 0 while i in unvisited: unvisited.remove(i) size += 1 i = i*p % n assert i == start counts[size] += 1 return tuple(sorted(counts.items())) def passes(counts, r): return any(degree % r == 0 and multiplicity % r for degree, multiplicity in counts) def dyadic_table(): P, r = 2**20, 3 primes = primes_upto(P) interval = [p for p in primes if p > P//2] menu = [q for q in primes if q % r == 1][:7] assert menu == [7,13,19,31,37,43,61] survivors = interval counts = [len(interval)] print("Cubic witnesses for 2^19 < p <= 2^20") print("k added conductor failures interval count/3^k") print(f"0 {'-':>15} {len(interval):>8} {len(interval):.2f}") for k,q in enumerate(menu,1): powers = {pow(a,r,q) for a in range(1,q)} for a in range(1,q): residue_fails = a in powers assert residue_fails == (pow(a,(q-1)//r,q) == 1) assert residue_fails == (not passes(frobenius_histogram(q,a),r)) survivors = [p for p in survivors if pow(p,(q-1)//r,q) == 1] counts.append(len(survivors)) print(f"{k} {q:>15} {len(survivors):>8} {len(interval)/r**k:.2f}") assert counts == [38635,12891,4232,1416,479,150,42,12] return survivors def prime_divisors(n): return [q for q in primes_upto(n) if n % q == 0] def compression_tests(): # In characteristic 2, Phi_(2m) reduces to Phi_m when m is odd. # All other ramified blocks have repeated roots and cannot be witnesses. single, pairs, successful_pairs, ramified = 0, 0, 0, 0 degrees = {n:sum(gcd(a,n) == 1 for a in range(1,n+1)) for n in range(1,50)} for r in (2,3,5,7): tagged = {} for n in range(1,43): eligible = [q for q in prime_divisors(n) if (q-1)%r == 0] if n%(r*r) == 0: eligible.append(r*r) tagged[n] = eligible[0] if len(eligible) == 1 else None if tagged[n] is not None: assert degrees[n] % degrees[tagged[n]] == 0 for p in primes_upto(37): if p==r: continue reduced = {n:n if n%p else n//2 for n in range(1,43) if n%p or (p == 2 and n%4 == 2)} hist = {n:Counter(dict(frobenius_histogram(m,p))) for n,m in reduced.items()} supplied = {n:tagged[n] is not None and passes(frobenius_histogram(tagged[n],p),r) for n in reduced} for n in reduced: assert not passes(hist[n].items(),r) or supplied[n] single += 1 for n,m in combinations(reduced,2): if reduced[n] == reduced[m]: continue counts = hist[n] + hist[m] if passes(counts.items(),r): assert supplied[n] or supplied[m] successful_pairs += 1 ramified += (n*m)%p == 0 pairs += 1 assert frobenius_histogram(35,3) == ((12,2),) assert frobenius_histogram(5,3) == ((4,1),) assert frobenius_histogram(4,3) == ((2,1),) assert frobenius_histogram(9,2) == ((6,1),) print(f"Compression verified on {single} single conductors and {pairs} two-conductor products.") print(f"{successful_pairs} two-conductor products passed, including {ramified} ramified cases.") print("Strict example: at p=3, Phi_5 Phi_10 has two quartics; the replacement Phi_5 has one.") print("Exceptional conductors: (r,p)=(2,3) uses Phi_4; (r,p)=(3,2) uses Phi_9.") def order_examples(): p = 41 for r,ell,h,multiplicity in ((2,97,96,1),(3,37,18,2),(5,61,10,6)): order = next(n for n in range(1,ell) if pow(p,n,ell) == 1) assert order == h and frobenius_histogram(ell,p) == ((h,multiplicity),) assert passes(((h,multiplicity),),r) for extension_degree in range(1,13): powers = frobenius_histogram(ell,p**extension_degree) split = gcd(h,extension_degree) assert powers == ((h//split,multiplicity*split),) assert passes(powers,r) == (extension_degree%r != 0) print(f"p=41, r={r}, ell={ell}: {multiplicity} factor(s) of degree {order} in Phi_{ell}.") def determinant(matrix): """Exact Bareiss determinant including row exchanges.""" a = [row[:] for row in matrix] sign, divisor = 1, 1 for i in range(len(a)-1): pivot_row = next((j for j in range(i,len(a)) if a[j][i]), None) if pivot_row is None: return 0 if pivot_row != i: a[i],a[pivot_row] = a[pivot_row],a[i] sign *= -1 pivot = a[i][i] for j in range(i+1,len(a)): for k in range(i+1,len(a)): numerator = pivot*a[j][k]-a[j][i]*a[i][k] assert numerator % divisor == 0 a[j][k] = numerator//divisor divisor = pivot return sign*a[-1][-1] def resultant(a,b): """Sylvester determinant for polynomials listed in descending degree.""" m,n = len(a)-1,len(b)-1 rows = [[0]*i+a+[0]*(n-1-i) for i in range(n)] rows.extend([[0]*i+b+[0]*(m-1-i) for i in range(m)]) return determinant(rows) def divisible(a,b,p): """Test divisibility modulo p; arguments are monic, descending degree.""" work = a[:] for i in range(len(a)-len(b)+1): lead = work[i] for j,c in enumerate(b): work[i+j] = (work[i+j]-lead*c) % p return not any(x%p for x in work) def cubic_period_examples(): cases = ((7, [1, 1, -2, -1], 49), (9, [1, 0, -3, 1], 81)) for conductor, coefficients, discriminant in cases: derivative = [(3-i)*c for i, c in enumerate(coefficients[:-1])] assert -resultant(coefficients, derivative) == discriminant for p in primes_upto(2000): if conductor % p == 0: continue _, a, b, c = coefficients root_count = sum(((x+a)*x*x+b*x+c) % p == 0 for x in range(p)) expected_split = p % conductor in (1, conductor-1) assert root_count == (3 if expected_split else 0) assert (root_count == 0) == passes(frobenius_histogram(conductor, p), 3) print("Both degree-three period polynomials match the complete factors at every unramified prime up to 2000.") def discriminant_example(): q, g = [1,0,1], [1,0,0,1,2] # Trial division by every monic polynomial up to half the degree is exact. for a in (q,g): for d in range(1,(len(a)-1)//2+1): for coeffs in product(range(3),repeat=d): assert not divisible(a,[1]+list(coeffs),3) f = [sum(q[i]*g[k-i] for i in range(len(q)) if 0<=k-i