David Gale, a wide-ranging mathematician who helped develop an algorithm for pairing individuals into couples that has also been used to match hospitals with fledgling doctors for medical residencies, died on March 7 in Berkeley. He was 86.

The cause was a heart attack, his family said.

Gale’s interests included mathematical games and puzzles and the creation of economic models, but he was widely recognized for work on the so-called stable marriage algorithm, a concept he developed in the 1960s with the economist and mathematician Lloyd S. Shapley.

The problem begins with the assumption that equal numbers of men and women are in search of potential partners. Is it possible to pair the individuals in such a way that all achieve a satisfactory match?

The solution developed by Shapley and Gale was to have each participant rank the members of the other sex in terms of desirability. The researchers then developed an algorithm that directed each participant to his or her next choice of partner, if rejected by the first or second choices.

The result was that everyone would be matched in a “stable” pairing.

The findings were published in 1962 in the American Mathematical Monthly, and were soon recognized as having broad applications to other situations.

More recently, a form of the algorithm has been used to assign students to public high schools in New York City. As applied to medical students and their residencies, it has been criticized for not taking hospital salaries into consideration, potentially stifling competition and affecting the way the free market might raise pay for residents, an issue that is controversial among economists who study the matching process.

In 2007, Gale took a fresh look at matching in another form: reports of imbalances in the number of sexual partners that men and women claim they have had in their lifetimes. When a federal survey found that American women reported a median of four partners and that the median reported for heterosexual men was seven, Gale expressed his incredulity and wrote a proof to explain why. Some had suggested that men’s traffic with prostitutes would account for the difference, but Gale believed that would have had a “negligible” effect on the numbers.

“Surveys and studies to the contrary notwithstanding,” he later told the New York Times, “the conclusion that men have substantially more sex partners than women is not and cannot be true for purely logical reasons.”

David Gale was born on Dec. 13, 1921, in Manhattan. He graduated from Swarthmore College before receiving his doctorate in mathematics at Princeton in 1949.

Gale’s marriage to Julie Skeby ended in divorce in 1974. He is survived by his companion, Sandra M. Gilbert, a poet and literary critic. They lived in Berkeley and Paris. He is also survived by three daughters, Karen, of Sacramento, Katharine, of Berkeley, and Kirsten Cutler of Santa Rosa; a sister, Ellen Dunning of Peabody, Mass; and two grandchildren.

With lower home prices, more Californians could afford a home purchase in the fourth quarter of 2018 compared to the previous quarter, but the California Association of Realtors reports higher interest rates lowered affordability from the previous year for most counties.