The Collatz Conjecture (3n + 1)

The Collatz Conjecture (3n + 1)

Graphed using Python!

Welcome to Nekoto's Brain Bash!

Lets make hard concepts easier for newbies!

What even is the Collatz Conjecture?

Its a Mathematical Conjecture (problem / issue / mystery) that usually is marked off by Mathematicians as a waste of time attempting to solve. Often advised to newer Mathematicians to steer away from this conjecture, and what has become, a joke in some senses, to more experienced Mathematicians.

What's interesting about it though, is the way it plots on a graph. Its pattern is completely random and when used in conjunction with a logarithm on the y-axis; results in Brownian motion.

All of this, whilst being composed solely of 2 rules, is impressive to say the least.

So lets take a look at it in a more interactive way - through our buddy Python!

Jupyter Notebooks!

This is the first time I've written a Jupyter Notebook, but they're such a fun and interactive way of both developing, and learning about something new! They also greatly help when revisiting old topics that you've forgotten in a more intuitive way!

So here's my Jupyter Notebook on the subject. Enjoy!

Just the Code

In case you would like just the code to try this out yourself:

import matplotlib.pyplot as plt

start_number = input("Input a starting number: ")

if start_number.isnumeric():
    start_number = int(start_number)
else:
    quit()

MAX_ITERATIONS = 200

new_number = start_number
repeat_pattern = [4, 2, 1]
test_pattern = []
plt.plot(0, start_number)
for i in range(MAX_ITERATIONS):
    prev_number = new_number
    if new_number % 2 == 0:
        new_number /= 2
    else:
        new_number = (3 * new_number) + 1

    if new_number in repeat_pattern:
        test_pattern.append(new_number)
    if len(test_pattern) >= 3:
        if test_pattern == repeat_pattern:
            break
        else:
            test_pattern.clear()

    plt.plot((i-1, i), (prev_number, new_number))

plt.show()