Skip to content

Instantly share code, notes, and snippets.

@bensonk
Created March 5, 2010 23:41
Show Gist options
  • Save bensonk/323300 to your computer and use it in GitHub Desktop.
Save bensonk/323300 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
sys.setrecursionlimit(10000)
max = 1000
memo = {}
def collatz(x):
global memo
if x in memo: pass
elif x % 2: memo[x] = x * 3 + 1
else: memo[x] = x / 2
def follow(x):
collatz(x)
if memo[x] > 1: follow(memo[x])
if len(sys.argv) > 1: max = int(sys.argv[1])
for i in range(2, max):
follow(i)
print("digraph collatz {")
for pair in memo.items():
print("\t%d -> %d;" % pair)
print("}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment