#!/usr/bin/env python ### wander5 -- more about Jim Propp's 2008.04.09 question # Follow the rounding rule but throw away ones on the right # (we know what's going to happen to them). def pr( a ): print a[0], "|", for x in a[ 1: ]: if x == 0: print "", else: print x, print # In this thing, a[0] accumulates stuff that falls off the left. def f( n ): a = [ 0, n ] widest = len(a) time = 0 while True: # pr( a ) for i in range( len(a)-1, -1, -1 ): if round( a[i] / 3.0 ) > 0.0: break b = [0] * ( i + 2 ) b[0] = a[0] for j in range( 1, i + 1 ): b[ j-1 ] += int( round( a[j] / 3.0 ) ) b[ j+1 ] = int( round( a[j] * 2.0 / 3.0 ) ) a = b widest = max( widest, len(a) ) time += 1 if i == 0 or a[1] <= 1 and a[2] <= 1: break # pr(a) return n - a[ 0 ], widest, time mn = mx = 0 for i in range( 2, 2000000 ): y, widest, time = f(i) y = float( y ) x = y - ( i * .5 ) if x <= mn: print "%3d" % i, "%+3.1f" % x, "%4d" % widest, "%4d" % time mn = x elif x > mx: print "%3d" % i, "%+3.1f" % x, "%4d" % widest, "%4d" % time mx = x