DEV Community

Cover image for Tupper's Self-Referential Formula: A Math Equation That Draws Itself!
bytecrafter
bytecrafter

Posted on

Tupper's Self-Referential Formula: A Math Equation That Draws Itself!

If I told you there is a mathematical inequality that, when plotted on a graph, produces an image of the formula itself, would you believe me?

This isn't sci-fi; it's the famous Tupper's Self-Referential Formula. For us developers, this formula isn't just math—it's essentially an incredibly elegant data decompression algorithm.

Let's dive into the logic behind this "magic."

1. Witness the Miracle

Jeff Tupper introduced this formula in 2001. Take a look at the inequality:

12<mod(y17217xmod(y,17),2) \frac{1}{2} < \left\lfloor \mathrm{mod}\left( \left\lfloor \frac{y}{17} \right\rfloor 2^{-17 \lfloor x \rfloor - \mathrm{mod}(\lfloor y \rfloor, 17)}, 2 \right) \right\rfloor

Looks intimidating, right?

💡 Quick Writer's Tip:
You might be wondering, did I type out that complex LaTeX string character by character while staring at Wikipedia? Absolutely not! As a developer who values efficiency, I actually found an image of the formula online and used a tool to convert it to editable LaTeX formula code.

I simply uploaded the image, and it automatically recognized the symbols and converted them into ready-to-use LaTeX code. If you plan on writing math-heavy articles on Dev.to, I highly recommend bookmarking this "screenshot-to-code" tool—it saves a ton of debugging time.

Now, back to the formula. It looks scary, but we are going to deconstruct it just like legacy code.

2. How Does It Work?

To see the "true face" of this formula, we need to plot points (x,y)(x, y) on a graph. If a point satisfies the inequality, we color it black; otherwise, we leave it white.

We plot within a specific range:

  • X-axis range: 0x1060 \le x \le 106
  • Y-axis range: kyk+17k \le y \le k + 17

Here, kk is a very, very massive constant (the full number is in the appendix because it's 543 digits long!).

When you iterate through xx at this specific height interval on the yy -axis, something magical happens: The generated graph renders the pixel map of the formula itself!

3. The Developer's Perspective

As developers, we can easily see through the "magic." This formula isn't sentient; fundamentally, it is a universal bitmap decoder.

Let's simplify the math. If you look closely at the exponent part:

17xmod(y,17) -17 \lfloor x \rfloor - \mathrm{mod}(\lfloor y \rfloor, 17)

And the y/17\lfloor y/17 \rfloor part, you'll realize this is actually performing bitwise operations.

  1. It's a 106 x 17 Pixel Grid: The number 1717 in the formula represents the height of the image (in pixels).
  2. It's Binary Encoding: The entire formula is basically checking: In the binary representation of the number y/17\lfloor y/17 \rfloor , is the (17x+y(mod17))(17x + y \pmod{17}) -th bit a 0 or a 1?
  3. The Constant K is the Data: That massive constant kk is actually the raw data. It's the image of the formula converted into binary (black=1, white=0) and then stitched together into one giant base-10 integer.

Conclusion: The formula is the "projector," and the constant kk is the "film reel." If you change kk to a different number, this exact same formula could draw a cat, a line of code, or anything else that fits within a 106x17 pixel grid.

4. Why is it called "Self-Referential"?

Strictly speaking, logicians might argue it's not truly self-referential (because it relies on the external constant kk to define the shape). However, it is still incredibly cool because it proves that any information—including the mathematical language used to describe the processing rules—can be encoded as a single pure number.

In Computer Science, this reminds us of the "Code is Data" philosophy found in Lisp and other homoiconic languages.

5. Python Implementation

Want to try it yourself? Here is the logic in Python to verify if a specific pixel should be "painted black":

def is_pixel_black(x, y):
    # Note: 'y' passed here should be the actual coordinate (k + offset)

    # Translating the core formula logic
    exponent = -17 * x - (y % 17)
    term = (y // 17) * (2 ** exponent)
    result = (term % 2)

    # In integer arithmetic, we just check if the bit is 1
    return result >= 0.5 
Enter fullscreen mode Exit fullscreen mode

Appendix: The Massive Constant K

For the formula to draw itself, kk must be equal to:

960 939 379 918 958 884 971 672 962 127 852 754 715 004 339 660 129 306 651 505 519 271 702 802 395 266 424 689 642 842 174 350 718 121 267 153 782 770 623 355 993 237 280 874 144 307 891 325 963 941 337 723 487 857 735 749 823 926 629 715 517 173 716 995 165 232 890 538 221 612 403 238 855 866 184 013 235 585 136 048 828 693 337 902 491 454 229 288 667 081 096 184 496 091 705 183 454 067 827 731 551 705 405 381 627 380 967 602 565 625 016 981 482 083 418 783 163 849 115 590 225 610 003 652 351 370 343 874 461 848 378 737 238 198 224 849 863 465 033 159 410 054 974 700 593 138 339 226 497 249 461 751 545 728 366 702 369 745 461 014 655 997 933 798 537 483 143 786 841 806 593 422 227 898 388 722 980 000 748 404 719

(Plug this number into the formula, and you get the formula back. Isn't math romantic?)

Top comments (0)