Latest revision |
Your text |
Line 44: |
Line 44: |
| 33: 1 | | 33: 1 |
| [[User:International Space Station|International Space Station]] ([[User talk:International Space Station|talk]]) 09:36, 25 January 2016 (UTC) | | [[User:International Space Station|International Space Station]] ([[User talk:International Space Station|talk]]) 09:36, 25 January 2016 (UTC) |
− |
| |
− | 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-ex')
| |
− |
| |
− | --[[Special:Contributions/198.41.238.106|198.41.238.106]] 01:31, 28 July 2021 (UTC)
| |
− |
| |
− | :If we're sharing code, I made a Python script that calculates a string for any given number and then graphs it:
| |
− |
| |
− | import matplotlib.pyplot as plt
| |
− | start_number = int(input("Enter a number:\n"))
| |
− | loop_count = 0
| |
− | full_list = [start_number]
| |
− | while start_number != 1:
| |
− | loop_count += 1
| |
− | if start_number % 2 == 0:
| |
− | start_number = int(start_number / 2)
| |
− | print(start_number)
| |
− | full_list.append(start_number)
| |
− | elif start_number % 2 == 1:
| |
− | start_number = int(start_number * 3 + 1)
| |
− | print(start_number)
| |
− | full_list.append(start_number)
| |
− | else:
| |
− | print("Error")
| |
− | print("Iterations: " + str(loop_count))
| |
− | plt.plot(full_list)
| |
− | plt.title('Graph (Linear)')
| |
− | plt.xlabel('Steps')
| |
− | plt.ylabel('Hight')
| |
− | plt.show()
| |