Simon Singh is a Cambridge-trained physicist, best-selling author, and huge fan of The Simpsons. In his new book, The Simpsons and Their Mathematical Secrets, he uncovers the cartoon’s hidden, complex math lessons, and argues that the classic show is really written by a bunch of math nerds.

Ten years ago, I suddenly realized that The Simpsons is not just the most loved sitcom in the history of television, but also the most mathematical. While watching an episode titled “The Wizard of Evergreen Terrace,” I glimpsed a direct reference to a notorious mathematical problem known as Fermat’s last theorem—the subject of my first book, Fermat’s Enigma.

That prompted me to investigate further, which in turn led to the discovery of countless examples of mathematical references throughout The Simpsons, ranging from pi to infinity. The equations have been carefully smuggled into scripts by a writing team that consists of several people who have backgrounds in mathematics, including a couple of PhDs.

So numerous are The Simpsons‘ mathematical references that I would argue that my book, The Simpsons and Their Mathematical Secrets, offers a thorough introduction to math—including some quite advanced areas of number theory and geometry.

To give you a sense of what you have been watching, but not seeing, here’s a sampling of five mathematical references that have appeared in The Simpsons:

1. Fermat’s Last Theorem states that it is impossible to find a whole number solution to a particular equation (if you want to see the gory details, then the equation is xn + yn = zn, for n>2). However, in “The Wizard of Evergreen Terrace” (1998), Homer appears to have written a solution on a blackboard. In fact, it is merely a very carefully crafted so-called “near miss” solution, created by writer David X. Cohen. His equation will fool most calculators, but a device capable of greater precision will find the error in the calculation.

2. Prime numbers hold a special place in the hearts of mathematicians, which is why writer Jeff Westbrook (an ex-Yale professor) inserted the number 8,191 onto the Jumbo-vision screen at Springfield stadium in “Marge and Homer Turn a Couple Play” (2006). Moreover, this is no ordinary prime number, because 8,191 = (2×2×2×2×2×2×2×2×2×2×2×2×2) – 1. This means that it is known as a Mersenne prime number, named in honor of the Frenchman Marin Mersenne.

3. The Springfield movie theater is called the Googolplex, named by writer Mike Reiss, who was a mathlete when he was a teenager. A googolplex refers to a phenomenally large number, namely 10googol. What is a googol? A googol is 10100. The search engine Google is named after this vast number, albeit with a slightly different spelling, in order to indicate that it offers its users access to vast amounts of information.

4. In the segment “Homer3“, part of “Treehouse of Horror VI,” we glimpse a reference to an unsolved problem concerning the relationship between hard problems (NP-type) and easy problems (P-type). Are these problems fundamentally different or is there a clever trick that would make the hard problems easy? Nobody knows the answer, and there is a $1 million prize for anyone who can solve the mystery. In the meantime, the writers of The Simpsons have signaled their gut instinct that the two types of problems are effectively the same, because one scene in “Homer3” shows the equation P=NP.

5. The writers of The Simpsons are nerds and they love to celebrate geek role models. Hence, “Homer3” contains an apparently random series of numbers and letters: “46 72 69 6E 6B 20 72 75 6C 65 73 21.” The letters, in fact, are actually hexadecimal (or base 16) numbers. Together, each pair of hexadecimal digits represents a character in ASCII (American Standard Code for Information Interchange), which is a protocol for converting letters and punctuation into numbers, largely for the benefit of computers. According to the ASCII protocol, 46 represents “F,” 72 represents “r,” and so on. Translated, the entire sequence reads as a bold proclamation in praise of geeks: “Frink rules!”

Related: 50 Funny Math Jokes