THE SIMPSONS AND THEIR MATHEMATICAL SECRETS. By Simon Singh. Bloomsbury. 272 pages. $26.

Did you know that Homer Simpson disproved Fermat's last theorem? He did, or so it seemed, when he scribbled "3987(12)+4365(12)=4472(12)" on a blackboard in a 1998 episode of "The Simpsons."

If Homer is right, then he has proved that the great 17th-century mathematician Pierre de Fermat was wrong in stating that the equation x(n)+y(n)=z(n) has no solution when x, y and z are positive whole numbers and n is a whole number greater than 2. (That would also mean British mathematician Andrew Wiles was wrong when he finally proved the theorem in 1995. Fortunately, he was not.)

Nor is Homer the only mathematical prodigy on "The Simpsons": In a 1993 episode, we learn that Apu Nahasapeemapetilon, proprietor of the Kwik-E-Mart in Springfield, has memorized pi to the 40,000th digit, which he correctly informs us is 1; and in 2010, Lisa becomes nearly unbeatable as a baseball manager by mastering complex statistics.

All this may come as a surprise to most "Simpsons" fans, who presumably do not tune in to ponder the mysteries of higher mathematics. But as British author Simon Singh shows in "The Simpsons and Their Mathematical Secrets," math is everywhere in the Simpsons' world, from references that flash across the screen in an eye blink (such as Springfield's Googolplex movie theater) to entire segments that explore deep mathematical concepts (like "Homer(3)" in 1995). Math is built into the show's DNA.

Not content merely to point out the mathematical references, Singh uses them as a starting point for lively discussions of mathematical topics, anecdotes and history. Even someone with no mathematical background will enjoy his accounts of the nature of infinity and the meaning of the number e, the life of the tragic genius Srinivasa Ramanujan and the obsessions of Bill James, the oracle of baseball statistics.

Perhaps the most surprising revelation is the composition of "The Simpsons" creative team, which during the years has included J. Stewart Burns, who has a master's degree in math from Harvard; David X. Cohen (master's in computer science, University of California, Berkeley); and Ken Keeler (Ph.D. in applied math, Harvard). Most astonishing is Jeff Westbrook (Ph.D., computer science, Princeton), who was an associate professor at Yale before he joined the team.

It is understandable that a group with such credentials would enjoy sprinkling the show with tidbits from their previous line of work. As Cohen explains, "It cancels out those days when I've been writing those bodily function jokes."

While the show allows for a wide range of improbable turns (Homer disappears into the third dimension, Lisa is rescued from an angry mob by Stephen Hawking), not everything is allowable: The characters must remain true to their personalities and the stories must follow their own inner logic, for a story free of the constraints of personality, logic and motivation is no story at all.