Editing Talk:710: Collatz Conjecture

Jump to: navigation, search
Ambox notice.png 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 *
+
from treelib import *
tree=Tree()
+
tree=Tree()
levels=int(input('Levels of tree? '))
+
levels=int(input('Levels of tree? '))
start=int(input('Starting number? '))
+
start=int(input('Starting number? '))
tree.create_node(start, start)
+
tree.create_node(start, start)
def f(x):
+
def f(x):
    global tree
+
    global tree
    if tree.depth(x)==tree.depth():
+
    if tree.depth(x)==tree.depth():
        return True
+
        return True
    else:
+
    else:
        return False
+
        return False
def collatz(x):
+
def collatz(x):
    if x%2==0 and x%3==1 and x!=4:
+
    if x%2==0 and x%3==1 and x!=4:
        return [(x-1)//3,x*2]
+
        return [(x-1)//3,x*2]
    else:
+
    else:
        return [x*2]
+
        return [x*2]
for i in range(levels):
+
for i in range(levels):
    N = list(tree.filter_nodes(f))
+
    N = list(tree.filter_nodes(f))
    for i in N:
+
    for i in N:
        c=collatz(i.tag)
+
        c=collatz(i.tag)
        for j in c:
+
        for j in c:
            tree.create_node(j,j,i)
+
            tree.create_node(j,j,i)
tree.show(line_type='ascii-ex')
+
tree.show(line_type='ascii')
 
 
--[[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()
 

Please note that all contributions to explain xkcd may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see explain xkcd:Copyrights for details). Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel | Editing help (opens in new window)

Template used on this page: