Editing Talk:710: Collatz Conjecture
Please sign your posts with ~~~~ |
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 47: | Line 47: | ||
I wrote a program to calculate the tree (Python 3.9), but you have to install treelib. Do this by typing 'pip install treelib' in the terminal. The program: | I wrote a program to calculate the tree (Python 3.9), but you have to install treelib. Do this by typing 'pip install treelib' in the terminal. The program: | ||
− | + | from treelib import * | |
− | + | tree=Tree() | |
− | + | levels=int(input('Levels of tree? ')) | |
− | + | start=int(input('Starting number? ')) | |
− | + | tree.create_node(start, start) | |
− | + | def f(x): | |
− | + | global tree | |
− | + | if tree.depth(x)==tree.depth(): | |
− | + | return True | |
− | + | else: | |
− | + | return False | |
− | + | def collatz(x): | |
− | + | if x%2==0 and x%3==1 and x!=4: | |
− | + | return [(x-1)//3,x*2] | |
− | + | else: | |
− | + | return [x*2] | |
− | + | for i in range(levels): | |
− | + | N = list(tree.filter_nodes(f)) | |
− | + | for i in N: | |
− | + | c=collatz(i.tag) | |
− | + | for j in c: | |
− | + | tree.create_node(j,j,i) | |
− | + | tree.show(line_type='ascii') | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |