In the winter of 1900, six-year-old Nicolas Slonimsky was introduced to the world of popular Russian ditties. “Little bird, what did you do? I drank vodka, so did you,” he would warble to enchanted relatives. Isabelle Vengerova, his aunt, who was a famous teacher at the St. Petersburg Conservatory, discovered earlier that year that he possessed perfect pitch, and began giving him lessons. She believed that he was destined for musical greatness. Although Slonimsky’s career as a musical prodigy never quite materialized, his early singing days made a lasting impression: Slonimsky was one of the first composers to explicitly decipher what made certain tunes irresistible. In the twenties, Oliver Sacks writes in “Musicophilia,” Slonimsky began creating musical patterns designed to “hook the mind and force it to mimicry and repetition.” In 1947, Slonimsky’s insights were released as a book, “Thesaurus of Scales and Melodic Patterns,” which would go on to become an important musical influence for composers from John Coltrane to Frank Zappa.

Slonimsky didn’t have a specific term for the types of compulsively catchy melodies he created, but one came into being in 1979: the German psychiatrist Cornelius Eckert described such tunes as “Ohrwürmer”—earworms. (The imagery has deep roots: “Musicophilia” notes that, centuries ago, an early folk-music manuscript referred to music that got stuck in your head as “the piper’s maggot.”) While there still isn’t a strict definition of what constitutes an earworm, it is generally considered to be a constant loop of fifteen to twenty seconds of music lodged in your head for at least a few hours, if not days—or, in severe cases, months.

Lauren Stewart, the founding director of the Music, Mind, and Brain program at Goldsmiths, University of London, first became fascinated by the phenomenon of earworms while driving to work one day in 2009. “There’s a radio station in the U.K. called 6Music, and they have a breakfast show where they have a feature called ‘Earworms,’ where people call in to say what music they woke up with in their heads,” she told me. At the time, she had been researching the cognitive neuroscience of music. Earworms, she realized, could offer insight into the way that the mind processes musical experiences. She asked the station to share its data. “Obviously people get earworms if they’ve just heard a song,” Stewart says. “What we wanted to know was: Were there any features over and above?” In other words, what gives birth to an earworm?

Shortly after her earworm-laden drive, Stewart began working with Daniel Müllensiefen, the co-director of the Music, Mind, and Brain program, and an expert in using computational tools to analyze the melodic features of music. As they waded through the 6Music database, they decided to gather additional data of their own, and created a site called the Earwormery, where people could share their earworm experiences. Over five years, their team compared thousands of songs reported as earworms to equally popular but less catchy tracks by analyzing underlying musical features, such as key and melody.

In still-to-be-published research, Stewart and Müllensiefen show that under the right circumstances, like hearing a song played multiple times in a day, “most songs can be earworms,” Stewart says. “But there do seem to be some that come up more than others.” A particular hallmark of earworms, Stewart discovered, is the presence of passages with closely spaced musical intervals and long notes. (In other words, a sequence in which the notes are close to each other on the music scale—such as C, C-sharp, and D—and each note is held for a moment before moving on to the next.) To help me understand what such a song would sound like, Stewart cited “Waterloo,” by ABBA. “In that passage”—here, she hummed the chorus for me—“see how closely spaced the notes are, and they are all of relatively long duration.” As I listened, sure enough, “Waterloo” burrowed into my head.

These modern insights about the construction of an earworm wouldn’t have surprised Slonimsky. Though the “Thesaurus of Scales and Melodic Patterns” is composed almost entirely of musical notes, with no explanatory text after its six-page introduction, a musician would quickly discern that many of the patterns match the ones identified by Stewart and Müllensiefen. Slonimsky also intuited that “these progressions can be transposed to any tonal center according to a composer’s requirement”—that the structure of an earworm is universal, from classical piano compositions to the bounciest pop song. It’s no wonder then, that while not every hit track is an earworm, some contemporary artists—including Lady Gaga, Queen, Kylie Minogue, and the Beatles, Stewart notes—have made extensive use of that magic formula of interval and length.

The ubiquity of modern music and the resulting proliferation of earworms raise another question, though: How do you dislodge one? In a study that Stewart and the psychologist Victoria Williamson just published in the journal PLoS ONE, they examined thousands of survey responses to see what, if anything, was an especially effective method. While people’s strategies for ridding themselves of unwanted aural guests fell into one of two broad categories—distraction or coping—the most successful way to remove an earworm, they found, was to deal with it head on, by intentionally listening to the song or singing it out loud, no matter how embarrassing the song.

But it may be futile to try to resist completely, if Stewart is correct about why we get earworms in the first place. In ongoing research with a team of neuroscientists at the University of Western Ontario, she says, “we’re working with the hypothesis that people are getting earworms to either match or change their current state of arousal—or a combination of the two.” She adds, “Maybe you’re feeling sluggish but need to take your child to a dance class, so it could be that an earworm pops into your hear that’s very upbeat, to help you along. Or working in reverse, can earworms act to calm you down?” It would explain why we sometimes get earworms even when we haven’t been listening to music at all, or why people who spend a great deal of time in nature often report beginning to hear every sound—wind blowing, leaves rustling, water rippling—as music, which their brain spontaneously plays over and over. Just as important, it would help explain why our brains often seem to linger on music that we don’t particularly care for. Slonimsky would have expected as much: he never argued that the passages in his thesaurus always produced the most beautiful or appealing music—only that they were effective, as anyone who has listened to Swedish pop would know all too well.

Photograph: Vintage Images/Getty.