Boiling an egg to perfection sounds like the simplest cooking task in the world, but as anyone who's boiled an egg knows, you can get surprisingly mixed results.

That's probably because you're not taking into account all the variables. But if you want the perfect boiled egg every time, quantum physicist Miłosz Panfil has your back.

He's designed a calculator that covers all your bases, even down to your altitude.

Everyone has their own tricks for boiling an egg. Making sure water is at a rolling boil before lowering the eggs in; piercing the "blunt" end with a thumb tack so the cooking egg can expand into the air bubble, preventing breakage; adding vinegar to soften the shell to make it easier to peel.

But getting the timing right is the kicker. According to Panfil, there are several variables. The size of the egg; the temperature of the egg before it is lowered into the water; and the altitude at which you are cooking the egg.

Eggs are actually relatively complicated. The yolks and whites inside have different fats and proteins, and cook at different rates, with the whites requiring less heat to set than the yolks.

This is why a soft-boiled egg, with a firm white and a soft yolk, is possible - although it requires a lower temperature or a shorter cooking time than a hard-boiled egg.

In addition, a smaller egg will take a shorter time to cook, while an egg straight from the fridge - rather than a room-temperature egg - will require more time.

Altitude is the one most people would never think about, but it's such an important consideration that the USDA Food Safety Inspection Service has an information sheet about it. Because atmospheric pressure decreases at higher altitudes as the air thins, the boiling point of water is lower.

This is because liquid boils when its internal vapour pressure is equal to the atmospheric pressure. If atmospheric pressure is lower, so too is the boiling point - so your egg will take longer to cook at higher altitudes.

This is why you can't boil an egg on Everest, as water's 70 degree boiling point is too low. And also why, if you were ejected into space without a space suit, your blood and saliva would start to boil.

The calculator is based on a formula for the perfect boiled egg, created by Charles DH Williams at the University of Exeter.

t = m * K * log(ywr * (Tegg - Twater)/(T - Twater))

Where t stands for time, m is mass, K is the thermal conductivity of the egg, T is the temperature between white and yolk, Tegg is the egg temperature, Twater is the water temperature and ywr is the yolk-white ratio.

Panfil's calculator relies on maintaining a consistent temperature, which may be difficult to do on a stovetop. It sets the maximum temperature between the white and yolk for the perfect soft-boiled egg at 65 degrees Celsius (149 Fahrenheit), and the maximum temperature for the perfect hard-boiled egg at 77 degrees Celsius (170.6 Fahrenheit).

Any higher, and the hydrogen sulfide in the white combines with ferrous sulfide in the yolk to produce that greenish coating you sometimes see on hard-boiled eggs. If you have a kitchen thermometer, you can experiment with different heat settings on your burners to find the sweet spot at which it hits max temperature.

You can have a play around with the calculator here, but, like most cooking, we suspect you're probably going to find the most success simply by experimenting and getting it wrong a lot to figure out the best way that works for you.

And if it doesn't work, you can get a little extra help from this calculator by Altitude.org, also based on Williams's work.