Skip to content

Instantly share code, notes, and snippets.

@rgrig
Created December 7, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrig/d22021369d38e5a863b2e115fb4848dd to your computer and use it in GitHub Desktop.
Save rgrig/d22021369d38e5a863b2e115fb4848dd to your computer and use it in GitHub Desktop.
import sys
M = 10
cache = [[None for k in range(2**b+1)] for b in range(M)]
def maxo(a, b):
if b is None:
return a
if a is None:
return b
return max(a, b)
def addo(a, b):
if a is None or b is None:
return None
return a + b
def T(b,k):
if k > 2 ** b:
return None
if cache[b][k] is not None:
return cache[b][k]
if b==0:
return 0
for l in range((k+1)//2, k+1):
cache[b][k] = maxo(cache[b][k], addo(l, addo(T(b-1, l), T(b-1,k-l))))
return cache[b][k]
for b in range(M):
sys.stdout.write('b={}:'.format(b))
for k in range(2 ** b + 1):
sys.stdout.write(' {:3}'.format(T(b,k)))
sys.stdout.write('\n')
for b in range(1,M):
sys.stdout.write('T({:2},{:3})={:5}\n'.format(b, 2**(b-1), T(b,2**(b-1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment