Plot of Collatz Orbits

This sketch is a plot of Collatz orbits:


Collatz orbits relate to the Collatz conjecture, which was posed by mathematician Lothar Collatz in 1937. Each positive integer has an orbit, which is a sequence of numbers generated according to the following rules:

  • Every positive integer, n, initiates its own orbit.
  • If a term in the orbit is even, divide it by 2 to get the next term.
  • If a term in the orbit is odd, multiply it by 3 and add 1 to get the next term, unless the term is 1.
  • If the term is 1, it terminates the orbit.

See Wikipedia: Collatz conjecture.

The Collatz conjecture postulates that all Collatz orbits are finite in length, ultimately terminating with 1.

The sketch is a plot of all coordinate pairs (x, y) where y is in the orbit of x, and (x, y) falls within the bounds of the canvas. Colors are assigned in a modular fashion, based on the value of y. The plot reveals that some y values are common to many orbits, while other y values are uncommon. To appreciate this, note that if a number, y, occurs in the orbit of x, then all the numbers in the orbit of y occur in the orbit of x. For example, the number 9232 occurs in 4011 of the orbits of the numbers 1 through 10000. Therefore, all the numbers in the orbit of 9232 occur in those 4011 orbits.

In the sketch, the terms are plotted as squares with a size of 1. Squares were used instead of points in order to work around a conundrum that arose in an earlier version of the sketch. See the thread Problem with Stroke Transparency in Plotting Points for a discussion of this problem.

Code for Python Mode:

# Plots each x, y where y is in the Collatz orbit of x
# By @javagar
# This code is hereby placed in the public domain
# See Wikipedia: https://en.wikipedia.org/wiki/Collatz_conjecture

# Modify these values to vary effects
square_size = 1
alph = 255

colors = [color(0, 0, 255, alph),
          color(255, 255, 0, alph),
          color(255, 0, 255, alph),
          color(0, 255, 0, alph),
          color(0, 255, 255, alph),
          color(255, 0, 0, alph),
          color(255, 255, 255, alph)
 ]

def setup():
    size(400, 400)
    background(0)
    noStroke()
    rectMode(CENTER)
    noSmooth()
    noLoop()
    
def draw():
    for x in range(width):
        c = collatz(x)
        for y in c:
            # Use a square to represent x, y
            # Choose colors by row
            fill(colors[y % 7])
            square(x, y, square_size)

def collatz(n):
    # Returns Collatz orbit for n, as a list
    c = [n]
    # Terminate orbit at 1
    while n > 1:
        if n % 2 == 0:
            # Even number
            n //= 2
        else:
            # Odd number
            n = 3 * n + 1
        c.append(n)
    return c

Code for p5.js Mode:

// Plots each x, y where y is in the Collatz orbit of x
// By @javagar
// This code is hereby placed in the public domain
// See Wikipedia: https://en.wikipedia.org/wiki/Collatz_conjecture

// Modify these values to vary effects
let square_size = 1
let alph = 255
let colors;

function setup() {
    createCanvas(400, 400);
    background(0);
    noStroke();
    rectMode(CENTER);
    noSmooth();
    noLoop();
    colors = [
          color(0, 0, 255, alph),
          color(255, 255, 0, alph),
          color(255, 0, 255, alph),
          color(0, 255, 0, alph),
          color(0, 255, 255, alph),
          color(255, 0, 0, alph),
          color(255, 255, 255, alph)
    ];
}
    
function draw() {
    for (x = 1; x < width; x += 1) {
        let c = collatz(x);
        for (let i = 0; i < c.length; i += 1) {
            let y = c[i];
            // Use a square to represent x, y
            // Choose colors by row
            fill(colors[y % 7]);
            square(x, y, square_size);
        }
    }
}

function collatz(n) {
    // Returns Collatz orbit for n, as a list
    let c = [n];
    // Terminate orbit at 1
    while (n > 1) {
        if (n % 2 == 0) {
            // Even number
            n = n / 2;
        } else {
            // Odd number
            n = 3 * n + 1;
        }
        c.push(n);
    }
    return c;
}

EDITED on July 20, 2021 to update a link.

2 Likes