Prime Minister Pedro Passos Coelho said a formula had been found after meeting with the leader of the rightist CDS-PP party three times in the past 24 hours to heal the most damaging political rift since the country received a bailout in 2011.

"A formula was found to maintain government stability," Passos Coelho told journalists after a crisis meeting with the president to discuss the political situation.

The resignations this week of finance minister Vitor Gaspar and foreign minister Paulo Portas, who also heads the CDS-PP, have threatened to deprive the government of a majority in parliament as the country goes through its deepest slump since the 1970s.

Passos Coelho said the solution would involve a "way to guarantee the political support of the CDS-PP of the government," adding that the resignation of Portas had been a personal decision.

Still, the prime minister gave no details of the agreement and the final outcome still depends on further negotiations with Portas in coming days. It is likely to include more ministerial positions for the CDS-PP, analysts said.

Any final agreement will also have to be approved by President Anibal Cavaco Silva, who will meet with all political parties starting on Monday.

Portuguese assets were highly volatile on the day but by the afternoon bond yields fell and stocks recovered most of their sharp losses from a day earlier as agreement appeared closer.

But, analysts say the crisis, whatever the outcome, is likely to deepen Portugal's economic challenges. Unemployment is at record highs near 18 percent and the country entered its third year of recession in 2013.

"Whatever the (political) scenario involved, the re-pricing of the country's risk premium will probably be permanent, at levels lower than the present ones but higher than the minimum achieved just before the occurrence of this episode," said Paula Carvalho, chief economist at Banco BPI, in a research note.

Gaspar, architect of the spending cuts and tax hikes required by Portugal's lenders in exchange for a bailout, quit as finance minister on Monday, citing an erosion in support.

Portas resigned the next day because he objected to the appointment of Treasury Secretary Maria Luis Albuquerque to replace Gaspar.

The threat of a prolonged crisis that could lead to elections in a few months has opened the possibility of delays to reforms and cost-cutting under the country's 78-billion-euro bailout.

DIFFICULT TALKS

"A negotiated solution in which Portas stays out of the government but the coalition survives would allow the CDS-PP leader to partially save face," said Antonio Barroso, a London-based political analyst at advisory firm Teneo Intelligence, in a research note. "However, the negotiations are likely to prove difficult."

The head of the Portuguese Industry Confederation piled pressure on the government to find a solution quickly. "The crisis has to be overcome in parliament, and we think the conditions for that to happen exist," Antonio Saraiva told Reuters.

In a sign of how widespread disenchantment with austerity has become in Portugal, Saraiva also said the country has to be given more time by creditors - the European Union and International Monetary Fund - to meet budget goals under their bailout program.

The returns investors demand to hold Portugal's 10-year bonds were about 16 basis points lower on the day at 7.34 percent. On Wednesday they surged to more than 8 percent - their highest since November.

Lisbon's PSI 20 <.PSI20> stock index closed 3.7 percent higher after sliding 5.3 percent the previous day, as hopes grew that the government will stay on and avert an early election.

The EU and IMF are due to start their next review of the economy on July 15, but that visit might now be delayed.

(Additional reporting by Sergio Goncalves and Andrei Khalip, Editing by Jeremy Gaunt, Ron Askew.)

By Shrikesh Laxmidas and Axel Bugge