Created
March 5, 2010 23:41
-
-
Save bensonk/323300 to your computer and use it in GitHub Desktop.
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
#!/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