Mercurial > hg > euler
view problem14.py @ 14:49c96972949d default tip
#50 rewrite of solution
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Mon, 01 Dec 2008 19:11:55 +0000 |
parents | 1ca695e32f66 |
children |
line wrap: on
line source
def even(x): return x/2 def odd(x): return 3*x + 1 def fn(x): if x % 2 == 0: return even(x) else: return odd(x) def lookup(x, cache): if not x in cache: cache[x] = lookup(fn(x), cache) + 1 return cache[x] mx = 0 ml = 0 cache = { 1: 1 } for rx in range(1, 1000001): l = lookup(rx, cache) if l > ml: mx = rx ml = l print "Best x is %d with len %d" % (mx, ml)