# Plot of Collatz Orbits

**URL:** <https://discourse.processing.org/t/plot-of-collatz-orbits/31354>\
**Category:** Gallery\
**Created:** [July 21, 2021, 1:42am UTC](https://discourse.processing.org/t/plot-of-collatz-orbits/31354 "2021-07-21T01:42:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 21, 2021, 1:42am UTC](https://discourse.processing.org/t/plot-of-collatz-orbits/31354/1 "2021-07-21T01:42:26Z")

</div>

This sketch is a plot of Collatz orbits:

 ![pyde_collatz](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5327eb290aa3e545a2ddc02819f082c3bbd6e62e.png)  
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](https://en.wikipedia.org/wiki/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](https://discourse.processing.org/t/problem-with-stroke-transparency-in-plotting-points/31313) for a discussion of this problem.

Code for Python Mode:

```auto
# 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:

```auto
// 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._
