Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active September 4, 2015 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgedmin/323897 to your computer and use it in GitHub Desktop.
Save mgedmin/323897 to your computer and use it in GitHub Desktop.
Project Euler problem 14
# refactoring of http://sandrotosi.blogspot.com/2010/03/project-euler-problem-14.html
cache = {1: 1}
def collatz(n):
if n not in cache:
if n % 2 == 0:
cache[n] = 1 + collatz(n/2)
else:
cache[n] = 1 + collatz(3*n + 1)
return cache[n]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment