There is a best way – mathematically– to pour your second cup of coffee, says a study called Recursive Binary Sequences of Differences that will appeal to anyone who is truly pernickety about their beverages.

But no one realised it until the year 2001, when Robert M Richman published his simple recipe in the journal Complex Systems. During the subsequent passage of nine years and billions of cups of coffee, the secret has been available to all.

"The problem is that the coffee that initially comes through the filter is much stronger than that which comes out last, so the coffee at the bottom of the pot is stronger than that at the top," says Richman. "Swirling the pot does not homogenise the coffee, but using the proper pouring pattern does."

Here's all you have to do. Prepare coffee – two cups' worth – in a carafe. Now get two mugs, call them A and B. Then: "If one has the patience to make four pours of equal volume, the possible pouring sequences are AABB, ABBA, and ABAB."

Choose ABBA.

That's it. You now have two nearly-identical-tasting cups of coffee.

Richmond tells what to do if you're pernickety: "If one wishes to further reduce the difference and has more patience, one can make eight pours of equal volume, four in each cup. The number of possible sequences is now 35." The optimal sequence, he calculates, is ABBABAAB.

And if you are more finicky than that, Richmond neglects you not. "With even more patience, one may make 16 pours, eight into each cup. There are now 6,435 possible pouring sequences." ABBABAABBAABABBA is the way to go.

This same blending problem crops up elsewhere in modern life: in distributing pigments evenly when mixing paint, and even in choosing sides for a basketball game. "Consider the fairest way for "captain A" and "captain B" to choose sides," Richman instructs. The traditional method – alternating the choices – leads to unequally strong teams. Instead, use the coffee recipe, which is "likely to result in the most equitable distribution of talent". Insist that captain A has the first, fourth, sixth, and seventh choices, while captain B has the second, third, fifth, and eighth choices."

The mathematics in this study looks at coffee production as a collection of "Walsh functions". These are trains of on/off pulses that add together in enlightening ways.

The monograph ends modestly, or perhaps realistically, with a wistful thought: "As is typically the case with fundamental contributions, scientifically significant applications of this work may not appear for some time."

Richman recently retired as a chemistry professor at Mount St Mary's University in Emmitsburg, Maryland. He now has more time to devote to this mixing business, with pleasure.

"It took me over 10 years to develop the mathematics to solve this problem, which is well outside of my primary area of expertise. I'm trying to find a classical number theorist who is willing to collaborate on the sequel: I think I can definitively establish the best way to pour three cups of coffee".

• Marc Abrahams is editor of the bimonthly Annals of Improbable Research and organiser of the Ig Nobel prize