view problem14.py @ 2:1ca695e32f66

Solutions from tungsten
author Dominic Cleal <dominic@computerkb.co.uk>
date Mon, 01 Dec 2008 10:57:01 +0000
parents
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)