Agile Coder Logo
Intermediate

Toothpick Sequence: Self-Similar Growth

Start with a single toothpick. Add new ones at every exposed endpoint. What emerges is a fractal structure that doubles in complexity each generation.

Smruti Ranjan
May 20, 2026
3 min read
#fractals#sequence#cellular-automata#p5js
Toothpick Sequence: Self-Similar Growth
p5.js sketch
Advertisement

The toothpick sequence is a mathematical and geometric pattern that involves repeatedly adding toothpicks to a set of existing toothpicks based on specific rules. It was popularized by the mathematician Martin Gardner in his column in the August 1971 issue of Scientific American.

A two-dimensional arrangement of toothpicks is constructed by the following iterative procedure.

  1. At stage 1, place a single toothpick of length 1 on a square grid, aligned with the y-axis.
  2. At each subsequent stage, for every exposed toothpick end, place a perpendicular toothpick centered at that end.
  3. The resulting structure has a fractal-like appearance.

Here is how we can simulate the sequence

js
let picks = [];

let len = 63;

let minX;
let maxX;

function setup() {
  createCanvas(600, 600);
  minX = -width / 2;
  maxX = width / 2;
  picks.push(new Toothpick(0, 0, 1));
}

function draw() {
  background(255);
  translate(width / 2, height / 2);
  let factor = float(width) / (maxX - minX);
  scale(factor);
  for (let t of picks) {
    t.show(factor);
    minX = min(t.ax, minX);
    maxX = max(t.ax, maxX);
  }

  let next = [];
  for (let t of picks) {
    if (t.newPick) {
      let nextA = t.createA(picks);
      let nextB = t.createB(picks);
      if (nextA != null) {
        next.push(nextA);
      }
      if (nextB != null) {
        next.push(nextB);
      }
      t.newPick = false;
    }
  }

  picks = picks.concat(next);
  if (frameCount > 200) {
    noLoop(); 
  }
}
↕ click to expand

Thats the main functions and the Class Toothpick that has been used here is as follows:

js
class Toothpick {

  constructor(x, y, d) {
    this.newPick = true;

    this.dir = d;
    if (this.dir == 1) {
      this.ax = x - len / 2;
      this.bx = x + len / 2;
      this.ay = y;
      this.by = y;
    } else {
      this.ax = x;
      this.bx = x;
      this.ay = y - len / 2;
      this.by = y + len / 2;
    }
  }

  intersects(x,y) {
    if (this.ax == x && this.ay == y) {
      return true;
    } else if (this.bx == x && this.by == y) {
      return true;
    } else {
      return false;
    }
  }

  createA(others) {
    let available = true;
    for (let other of others) {
      if (other != this && other.intersects(this.ax, this.ay)) {
        available = false;
        break;
      }
    }
    if (available) {
      return new Toothpick(this.ax, this.ay, this.dir * -1);
    } else {
      return null;
    }
  }

   createB(others) {
    let available = true;
    for (let other of others) {
      if (other != this && other.intersects(this.bx, this.by)) {
        available = false;
        break;
      }
    }
    if (available) {
      return new Toothpick(this.bx, this.by, this.dir * -1);
    } else {
      return null;
    }
  }

  show(factor) {
    stroke(0);
    if (this.newPick) {
      stroke(0, 0, 255);
    }
    strokeWeight(1 / factor);
    line(this.ax, this.ay, this.bx, this.by);
  }
}
↕ click to expand

Here are some basic facts about the toothpick sequence:

  1. Growth Rate: The toothpick sequence exhibits exponential growth. As each step generates more toothpicks, the number of toothpicks in the sequence grows rapidly.

  2. Self-Similarity: At each step of the sequence, the overall structure retains self-similarity. That means the pattern at a larger scale resembles the pattern at a smaller scale.

  3. Infinitely Many Toothpicks: The toothpick sequence continues indefinitely, and there is no known end to the number of toothpicks that can be generated.

  4. Fractal Nature: The toothpick sequence displays fractal properties due to its self-replicating and self-similar nature.

  5. Unpredictability: Although the rules for generating toothpicks are simple, the resulting patterns can be highly unpredictable and complex, often leading to unexpected formations.

  6. Mathematical Exploration: The toothpick sequence has attracted the attention of mathematicians and researchers, who study its properties and analyze its growth behavior.

  7. Mathematical Notation: The toothpick sequence is sometimes represented using a notation system to describe the number of toothpicks at each step. For instance, the nth step may be denoted as T(n).

  8. Open Question: Despite its simplicity, some aspects of the toothpick sequence remain challenging to understand fully. As a result, there might be unsolved questions and conjectures related to its growth and behavior.

  9. Variants and Extensions: Over time, variations of the toothpick sequence have been proposed, exploring different ways of adding and arranging toothpicks, leading to diverse patterns and structures.

  10. Recreational Mathematics: The toothpick sequence is often considered a topic in recreational mathematics, as it offers an intriguing visual representation of how simple rules can create complex and captivating patterns.

Overall, the toothpick sequence serves as an interesting example of how basic mathematical rules can lead to fascinating and intricate structures, sparking curiosity and interest among mathematicians and enthusiasts alike.

Advertisement

Behind-the-build, in your inbox.

New tutorials, book updates, and behind-the-scenes notes from the studio. No schedule, no spam.

Comments

Leave a comment

Comments are moderated before appearing.