Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Created September 1, 2015 16:11
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 anirudhjayaraman/777e5c2680fd62c8de82 to your computer and use it in GitHub Desktop.
Save anirudhjayaraman/777e5c2680fd62c8de82 to your computer and use it in GitHub Desktop.
Project Euler Problem 14 - Smart Method
collatz = {1:1}
def Collatz(n):
global collatz
if not collatz.has_key(n):
if n%2 == 0:
collatz[n] = Collatz(n/2) + 1
else:
collatz[n] = Collatz(3*n + 1) + 1
return collatz[n]
for j in range(1000000,0,-1):
Collatz(j)
print collatz.keys()[collatz.values().index(max(collatz.values()))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment