I've had four coffees today: one right after breakfast, followed by chasers whenever I felt I was flagging (but probably just wanted a distraction). It looks like I've been doing this all wrong.

A new algorithm developed by scientists in the US Army reveals there's some complicated mathematics involved in getting the maximum safe benefit from a caffeine dosage – and when done right, it's something that delivers measurable results.

"We found that by using our algorithm, which determines when and how much caffeine a subject should consume, we can improve alertness by up to 64 percent, while consuming the same total amount of caffeine," says biotechnology researcher Jaques Reifman from the US Army's Advanced Technology Research Centre in Ft. Detrick, Maryland.

"Alternatively, a subject can reduce caffeine consumption by up to 65 percent and still achieve equivalent improvements in alertness."

To work the numbers, Reifman's team pulled data from a number of existing experimental studies of sleep loss, in which caffeine dosing strategies were compared against psychomotor vigilance task (PVT) performance – tests that measure participants' reaction times.

Previous work by Reifman and fellow researchers had found that PVT could be optimised by what they call their Unified Model of Performance (UMP), which predicts the effects of varying doses of caffeine on sleep loss as a function of time of day, depending on variables like how much sleep you've had.

This tool was already available as a free web app called 2B-Alert, and in the team's new paper, they explain how the same algorithms can be tailored to any individual for a mobile app that basically predicts exactly how much coffee you should be drinking for maximum buzz and focus.

"Our algorithm is the first quantitative tool that provides automated, customised guidance for safe and effective caffeine dosing to maximise alertness at the most needed times during any sleep-loss condition," says Reifman.

Unfortunately, this customisable mobile version of 2B-Alert isn't available to the public yet.

Before that happens, the algorithm is being assessed with soldiers in training, after which it's possible the Army might choose to licence the technology for any uses outside the military.

Hopefully they do, because it's not just coffee-loving service folk who could stand to benefit from knowing the perfect time to dose up, which is something the researchers are well aware of.

"Eighty percent of the US population consumes about two cups of coffee a day, and often more when we feel fatigued and desire an alertness jolt," Reifman told Digital Trends.

"Forty percent of [servicemen and women] sleep less than five hours per night on a consistent basis. In the civilian side, the algorithm has wide applicability for shift workers in the transportation industry, medical caregivers, firefighters, students, [and others]."

Just when all those people will get a chance to optimise their caffeine fix is anybody's guess, but your tax dollars went into building the ultimate coffee-drinking companion app, and hopefully we don't have to wait too long.

The findings are reported in the Journal of Sleep Research.