"""Rational tests of the gap integral and cap bounds for the stopped construction.""" from argparse import ArgumentParser from fractions import Fraction as R from functools import lru_cache from math import comb @lru_cache(None) def covered(n, u, s): if not n: return R(0) if n == 1: return R(u <= s) return sum((-1)**j * comb(n,j) * max(R(0), u-j*s)**(n-1) for j in range(n+1)) / u**(n-1) @lru_cache(None) def long_gap_integral(n, u, s): if n == 1: return max(R(0), 2*s-u) if u > s else R(0) out = R(0) for j in range(n): upper = min(2*s, u-j*s) if upper <= s: continue def primitive(w): return (2*s-u+j*s)*w**(n-1)/(n-1)+w**n/n area = primitive(u-j*s-s) - primitive(u-j*s-upper) out += (-1)**j * comb(n-1,j) * area return n*(n-1)*out/u**(n-1) def total(n, circles, s, split=False): if not circles: return R(n == 0) out = R(0) def visit(i,left,rows,weight): nonlocal out if i == len(circles): if left: return p = [covered(l,u,s) for l,u in zip(rows,circles)] if split: q = sum(long_gap_integral(l,circles[j],s) * product_except(p,j) for j,l in enumerate(rows)) else: q = product_except(p,-1) out += weight*q return for l in range(1,left-(len(circles)-i-1)+1): visit(i+1,left-l,rows+[l],weight*comb(left,l)*circles[i]**l) visit(0,n,[],R(1)) return out def product_except(values, skip): result = R(1) for j,v in enumerate(values): if j != skip: result *= v return result parser = ArgumentParser() parser.add_argument("--original", action="store_true", help="use the earlier maximum-gap bound") args = parser.parse_args() tests = 0 for circles in [(R(1),),(R(3,16),R(13,16)),(R(1,2),R(1,2)), (R(1,9),R(2,9),R(2,3)),(R(1,4),)*4]: for s in [R(1,5),R(1,4),R(2,7)]: cumulative = R(0) for m in range(1,16): empty_hit = sum(u*total(m,circles[:j]+circles[j+1:],s) for j,u in enumerate(circles) if u <= s) transition = total(m+1,circles,s)-total(m,circles,s) split = total(m,circles,s,True) assert split >= 0 and split == transition-empty_hit cumulative += split assert cumulative <= 1 tests += 1 if len(circles) == 1: assert cumulative == total(16,circles,s) print(tests, 'exact last-gap integrals agree with successive success differences') for h in (R(3,25),R(1,8),R(1,25),R(1,100)): s = h*R(999,4000 if args.original else 3000) rho = 1-s # For n>=1 the single-time tail majorant is # n*rho**(n-1)+(1/s)*rho**n. Its first downward crossing of one # minimizes the sum of 1 before truncation plus the geometric tail. n = int(1/s) while (n*rho**(n-1) + rho**n/s) > 1: n += 1 mean = n + n*rho**(n-1)/s + 2*rho**n/s**2 cap = 2+mean/2 ceiling = (cap.numerator+cap.denominator-1)//cap.denominator print('gap',h,'s',s,'cut',n,'mean upper',float(mean), 'cap upper',float(cap),'admissible cap',ceiling) assert cap < ceiling