Whether it's landing your dream job or getting the girl, a basic mathematical principle can help you in almost any situation.

That's according to Hannah Fry, a mathematician at the UCL Centre for Advanced Spatial Analysis in London and author of the new book "The Mathematics of Love."

She describes the "stable marriage problem," or the challenge of matching two entities so that neither would be better off in another match, and explains the Gale-Shapley matching algorithm often used to solve it. Exploiting this algorithm can be a great strategy for getting what you want.

Here's how it works: Fry uses the example of three boys talking to three girls at a party. Each participant has an ordered list of who is most suitable to go home with.

If this was a 1950s-style dating scenario in which the boys approached the girls, each boy would hit on his top-choice girl, Fry says. If a girl has multiple offers, she would choose the boy she preferred most, and if a boy were rejected, he would approach his second-choice girl.

The result is pretty great for the boys. Each gets his first- or second-choice partner, and there is no way the boys could improve, because their top choices have said yes or already rejected them.

The girls fare relatively worse, however, having paired up with their second- or third-choice partners.

Fry writes:

Regardless of how many boys and girls there are, it turns out that whenever the boys do the approaching, there are four outcomes that will be true:

1. Everyone will find a partner.

2. Once all partners are determined, no man and woman in different couples could both improve their happiness by running off together.

3. Once all partners are determined, every man will have the best partner available to him.

4. Once all partners are determined, every woman will end up with the least bad of all the men who approach her.

Essentially, whoever does the asking (and is willing to face rejection until achieving the best available option) is better off. Meanwhile, the person who sits back and waits for advances settles for the least bad option on the table.

The Gale-Shapley matching algorithm applies to plenty of situations beyond weekend hookups — including, say, hiring.

For example, a hiring manager who posts a job listing and lets the résumés roll in ultimately hires the best of the candidates who applied. But of course, that's a limited pool. On the other hand, a hiring manager who reaches out to the best professionals in the field and ends up with his or her third choice is still more likely to have a better candidate.

By the same token, a job seeker who approaches all the companies he or she wants to work for, starting with the most desirable, ends up with the best available employer.

The US National Resident Matching Program uses this strategy to match doctors with hospitals so that everyone is happy. Prior to the '50s, Fry says, hospitals reached out to the students they wanted, and the students accepted the least bad offers. But the organizers realized that doctors often had to relocate and weren't always happy with their options. To create a better system, they decided to flip the scenario and let doctors approach the hospitals they liked best.

Fry says the algorithm has been similarly applied to the assignments of dental residents, Canadian lawyers, and high-school students.

"Regardless of the type of relationship you're after," Fry concludes, "it pays to take the initiative."

Watch Fry's TED Talk on the mathematics of love: