Years ago, I heard that if you had an annoying song stuck in your head, the best way to get rid of it was to sing the entire theme song to Gilligan's Island to yourself. There was something about the length of the song that was just right to push the offending song out of your head. Unfortunately, it sometimes backfired and Gilligan just took the other song's place.

Well, now, according to researchers, if you have a song stuck in your head, the best way to get rid of that 'earworm' might be to get your brain working on something else entirely — such as a mental puzzle or reading a good book. However, don't go for anything too hard, though, because that might actually make it worse.

[ Related: The brainy science of finding your car keys ]

"The key is to find something that will give the right level of challenge," said Dr. Ira Hyman, a professor of psychology at Western Washington University who studies memory of song lyrics. "Something we can do automatically like driving or walking means you are not using all of your cognitive resource, so there is plenty of space left for that internal jukebox to start playing."

"Likewise, if you are trying something too hard, then your brain will not be engaged successfully, so that music can come back. You need to find that bit in the middle where there is not much space left in the brain," he added. "It is like a Goldilocks effect — it can’t be too easy and it can’t be too hard, it has got to be just right."

[ More Geekquinox: Black hole whips companion around at amazing speed ]

Hyman and his colleagues had volunteers sit down and work through brain puzzles while they played music for them — The Beatles, Lady Gaga and Beyoncé to be specific. They found that solving simple mazes didn't have much effect, but moderately-tough Sudoku puzzles and five-letter anagrams were very effective at keeping these catchy tunes from getting stuck in the volunteers' heads.

"Music is relatively harmless but easy to start," said Hyman. "Choruses tend to get stuck in your head because they are the bit we know best and because we don’t know the second or third verse, the song remains unfinished. Unfinished thoughts are more likely to return."

Whose songs were most likely to get stuck in people's heads? When it comes to modern artists at least, that title goes to Lady Gaga.

Read more from Yahoo! News