The Collatz Conjecture is a mathematical conjecture that states if you take any number and apply a very simple set of transformations, you will eventually reach 1. These simple transformations are:

  • If the number is even, divide the number by two. (n / 2)
  • If the number is odd, multiply the number by three and then add one. (3n + 1)

By taking any number and applying these transformations, over and over, you will eventually get to the number 1. The below drawing is a visualization of the Collatz Conjecture.

-0.170.19
113
#4aa2ec#4aa2ec#0b0b0b
2884
1

Turn this down low when making other changes because it significantly increases the time needed to calculate.

Do not use the slider within the color picker while this is high.

500

Adjust the settings above to generate your own Collatz Conjecture visualization. Every fifth line is drawn with a secondary color, for a little extra style.

The program goes through the Number range of numbers, starting from the Starting Number. The first number, by default, is 1. 1 is odd, so 3x+1 applies. 1 times 3 plus 1 is two. Two is even, so the number is divided by 2 (n / 2). Which leads us to 1.

This increments through numbers, one at a time until it reaches the top of the number range. For very large numbers, this process is very long and often the numbers go down and up as they are calculated to be even and odd.

For each number, a list is created to track the values the number went through. When it is finished, it draws a line in the reverse of the list of numbers. This always starts at 1 and leads eventually to the original number.

The visualization draws a line for each "entry" in the list. The line will then either turn left or right, determined by whether the number is even or odd.

This creates a branching, with all numbers starting from the same place and turning left and right as the line for each number reaches it's final (and first) value

A rendering of a collatz conjecture visualization
A rendering of a collatz conjecture visualization