Skip to content

Instantly share code, notes, and snippets.

@galenseilis
Created October 14, 2022 15:29
Show Gist options
  • Save galenseilis/b1eee59bf2ebd3cb0c727d8002c252b7 to your computer and use it in GitHub Desktop.
Save galenseilis/b1eee59bf2ebd3cb0c727d8002c252b7 to your computer and use it in GitHub Desktop.
cache = {1: 1}
def collatz_count(n):
if n not in cache:
if n % 2 == 0:
cache[n] = 1 + collatz_count(n // 2)
else:
cache[n] = 1 + collatz_count(3 * n + 1)
return cache[n]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment