Tupper’s Self-Referential Formula 🧮

Oct 15, 2025 min read

Playground

Have fun - https://aankitdas.github.io/tuppers-formula/

Overview

Tupper’s self-referential formula is a famous mathematical curiosity that can graph itself when plotted in a specific way. It was created by Jeff Tupper in 2001 and is often cited as a striking example of self-reference in mathematics. The formula is:

$$ \frac{1}{2} < \left\lfloor \text{mod} \left( \left\lfloor \frac{y}{17} \right\rfloor \cdot 2^{-17 \lfloor x \rfloor - \text{mod}(\lfloor y \rfloor, 17)}, 2 \right) \right\rfloor $$

Where the variables x and y represent coordinates on a Cartesian plane. The fascinating part is that when the formula is plotted for a large constant 𝑘, it reproduces the formula itself.


How the Code Works

1. Set Up the Canvas/Grid

The formula works by evaluating each pixel as either on or off, depending on the inequality. Typically:

  • Width = 106 pixels (for one horizontal repetition of the formula)
  • Height = 17 pixels per vertical block
  • Y-axis is scaled by a very large constant 𝑘 (the “magic number”) that encodes the image.
width = 106
height = 17
k = <very_large_integer>

2. Loop Through Coordinates

I loop over every pixel (x, y) in the grid:

for y in range(height):
    for x in range(width):
        # Evaluate the formula

For each (x, y):

  • Compute a transformed y-coordinate based on k.
  • Use integer division and modulo to check if the pixel should be filled.
  • If the inequality is True, color the pixel black; otherwise, leave it white.

3. Evaluate the Formula

The pixel evaluation involves:

  • Integer division (//) to break y into chunks
  • Bitwise checks or modulo operations to extract individual bits
  • Exponentiation (2^n) to isolate the correct bit
if 0.5 < ((y // 17) // (2 ** (17*x + y % 17))) % 2:
    draw_pixel(x, y)

4. Render the Output

After all pixels are evaluated:

  • Draw the black/white pattern on a canvas
  • Used libraries like matplotlib, Pillow, or numpy for rendering

Example in Python using Pillow:

from PIL import Image

img = Image.new('1', (width, height))  # '1' for 1-bit pixels
pixels = img.load()

for y in range(height):
    for x in range(width):
        if 0.5 < ((y // 17) // (2 ** (17*x + y % 17))) % 2:
            pixels[x, y] = 1  # black
img.show()

How It Becomes Self-Referential

The formula is encoded in the large constant 𝑘. When plotted:

  • Each bit of 𝑘 corresponds to a pixel on the grid
  • The resulting image draws the formula itself
  • Changing k allows encoding any bitmap image of the same width

Future Extensions

  • Animate the plotting process for educational demos