-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollatz_graph.py
58 lines (48 loc) · 1.52 KB
/
collatz_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import argparse
import os
doc_content = ('''digraph {\n
node[style=filled,color=".7 .3 1.0"];\n
1\n
node[style=filled,color=".95 .1 1"];\n''')
def f(n):
if n % 2 == 0:
return int(n / 2)
else:
return int(3 * n + 1)
def writeDotfile(filename, limit, explored):
dotfile = open(filename, "w")
dotfile.write(doc_content)
for n in range(2, limit):
while n not in explored:
dotfile.write(str(n) + " -> ")
explored.add(n)
n = f(n)
dotfile.write(str(n) + ";\n")
dotfile.write("}\n")
def createSvg(dotfile, base, program):
command = program + " -Tsvg " + dotfile + " -o " + base + ".svg"
print("Execute command: %s" % command)
os.system(command)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Graph for small Collatz sequences")
parser.add_argument(
"-f",
"--file",
dest="filename",
default="collatz-graph.gv",
help="write dot-FILE",
metavar="FILE",
)
parser.add_argument(
"-p",
"--program",
dest="program",
help="dot, neato, twopi, circo, fdp, sfdp, osage",
metavar="PROGRAM",
default="dot",
)
parser.add_argument("-n", dest="limit", default=20, type=int, help="limit")
args = parser.parse_args()
writeDotfile(args.filename, args.limit, set([1]))
# svg2png(url="./collatz-graph.svg", write_to='output.png')
createSvg(args.filename, os.path.splitext(args.filename)[0], args.program)