Created
September 9, 2011 14:53
-
-
Save msalahi/1206433 to your computer and use it in GitHub Desktop.
oheyandy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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