"""Check the field-note construction at sample sizes with exact arithmetic. Polynomials are coefficient tuples, constant coefficient first. """ from functools import cache def trim(a): while a and not a[-1]: a.pop() return tuple(a) def combine(a, b, sign=1): c = list(a) + [0] * max(0, len(b) - len(a)) for k, x in enumerate(b): c[k] += sign * x return trim(c) def product(a, b): if not a or not b: return () c = [0] * (len(a) + len(b) - 1) for i, x in enumerate(a): for j, y in enumerate(b): c[i+j] += x*y return trim(c) def divide(a, b): """Integer quotient and remainder for a monic divisor.""" assert b[-1] == 1 r = list(a) q = [0] * max(0, len(a) - len(b) + 1) while len(r) >= len(b): k = len(r) - len(b) q[k] = r.pop() for i, x in enumerate(b[:-1]): r[k+i] -= q[k]*x return trim(q), trim(r) @cache def cyclotomic(n): a = (-1,) + (0,)*(n-1) + (1,) for d in range(1, n): if n % d == 0: a, r = divide(a, cyclotomic(d)) assert not r return a def check(n): phi = cyclotomic(n) def reduce(a): return divide(a, phi)[1] def mul(a, b): return reduce(product(a, b)) def power(a, k): b = (1,) while k: if k & 1: b = mul(b, a) a = mul(a, a) k >>= 1 return b def z(k): return reduce((0,)*(k % n) + (1,)) j = combine(z(1), z(-1), -1) m = 4*n*n a = combine((m*m,), mul(j, j), -1) u, ubar = combine((m,), j), combine((m,), j, -1) b, bbar = mul(u, u), mul(ubar, ubar) assert mul(b, bbar) == mul(a, a) an = power(a, n) d, dbar = combine(an, power(b, n), -1), combine(an, power(bbar, n), -1) for s in range(n): assert combine(mul(z(s), d), mul(z(-s), dbar)), (n, s) print(f'N={n}: cyclotomic degree {len(phi)-1}, {n}/{n} last residues nonzero') def check_five(): """Direct deck checks for GPT-6.1 Terra's five-place example.""" from itertools import product # (a,b) means a+b*t, with t²=1−t. A = [(17, 2), (10, 3), (4, -4), (4, -4), (10, 3)] B = [(5, -2), (2, -3), (10, -2), (14, 6), (14, 1)] gaps = [(17500,10000), (-9250,-5250), (-22750,-14000), (-4750,-3500), (19250,12750)] assert len(set(A)) < len(set(B)) == 5 def reading(weights, shifts): real = radical = 0 for x in range(5): a, b = 1, 0 for s in shifts: c, d = weights[(x+s) % 5] a, b = a*c+b*d, a*d+b*c-b*d real += a radical += b return real, radical for k in range(1, 6): for u in product(range(5), repeat=k-1): a, b = reading(A, (0,*u)) c, d = reading(B, (0,*u)) assert (a-c,b-d) == ((0,0) if k < 5 else gaps[sum(u)%5]) assert (0,0) not in gaps print('Terra five-place example: all 781 ordered probes pass') if __name__ == '__main__': for n in (3,4,5,6,7,8,9,10,11,12,15,16,24,25,72): check(n) check_five()