"""Exact periodic tail identities. Small tiles only; no large census.""" from fractions import Fraction as Q from bisect import bisect_left from math import prod import json def check(W,openers,q,C): assert W%q==0 and openers==sorted(set(openers)) and openers vals=[];state=Q(0) for r in range(1,q+1): state+=Q(q,len(C))*(r%q in C)-1 vals.append(state) assert state==0 avg=sum(vals,Q(0))/q F={r%q:vals[r-1]-avg for r in range(1,q+1)} gaps=[b-a for a,b in zip(openers,openers[1:]+[openers[0]+W])] assert sum(gaps)==W R=Q(sum(g*g for g in gaps),2*W) phase=Q(0); byend={11:0,17:0,29:0} for g,b in zip(gaps,openers[1:]+[openers[0]+W]): phase+=Q(g,W)*F[(b+2)%q] if b%30 in byend:byend[b%30]+=g extended=[a-2*W for a in openers]+[a-W for a in openers]+openers+[a+W for a in openers] allsum=0;csum=0;nc=0 for o in range(W): # strict a+2>j&1] check(W,A,30,{1,19});exhaustive+=1 # General residue sets, even period and arbitrary (not just odd) openers. general=0 for W,q,A in [(12,3,[0,2,7]),(20,5,[1,9,19]),(24,6,[2,3,10,17])]: for mask in range(1,1<>r&1});general+=1 # Equal opener counts in the three classes do not force the constant 6. examples=[] for A in [[11,17,29],[11,17,299]]: R,E,w=check(300,A,30,{1,19});examples.append(str(E-R)) assert examples[0]!=examples[1] # Endpoint convention control: backward a<=o gives R-1/2, not R+1/2. A=[11,17,29];W=30 ext=[a-W for a in A]+A+[a+W for a in A] from bisect import bisect_right back=sum(o-ext[bisect_right(ext,o)-1] for o in range(W)) R,_,_=check(W,A,30,{1,19}) assert Q(back,W)==R-Q(1,2) print(json.dumps({'status':'PASS','arithmetic':'exact rational','tiles':rows, 'exhaustive_periodic_sets':exhaustive,'general_residue_cases':general, 'equal_count_counterexample_offsets':examples,'inclusive_backward_offset':'-1/2', 'scope':'Exact identities and small finite tiles only; no assertion that the actual class offset tends to 6 or that rough/anchored conditioning disappears.'},indent=2))