Skip to content

Instantly share code, notes, and snippets.

@msalahi
Created September 9, 2011 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msalahi/1206433 to your computer and use it in GitHub Desktop.
Save msalahi/1206433 to your computer and use it in GitHub Desktop.
oheyandy
import time
bound,mem = 1000000,{}
def collatz(n):
if n==1: return [1]
else: return [n] + collatz(n/2 if (n%2==0) else 3*n+1) if (n>1) else [1]
def collatzCount(n):
if n not in mem: mem[n]= 1 + collatzCount(n/2 if (n%2==0) else 3*n+1) if (n>1) else 1
return mem[n]
time1 = time.time()
print len(collatz(max(((i,collatzCount(i)) for i in xrange(1,bound)),key=lambda x: x[1])[0]))
print time.time()-time1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment