Stefan Mandel had a problem. In the 1960s, he lived under the oppressive regime of communist Romania. It was a problem he shared with millions of people, but Mandel, a trained economist with a head for numbers, came up with a rather novel way out of it. He decided he was going to win the lottery.

With the Powerball jackpot swelling to $768 million this week — the third largest jackpot in U.S. history — you, too, decided you were going to win the lottery. But just one winning ticket was sold in Wisconsin, Powerball officials said early Thursday morning, leaving the disappointed losers to commiserate on Twitter.

Mandel had a better way of making that happen than a rabbit’s foot, bibliomancy or knocking on wood.

He had an algorithm.

Mandel calculated that if he bought tickets with certain blocks of numbers, he could guarantee a second-place win. He convinced two friends — his first “investors” — to go in on the plan with him.

They didn’t win second place; they won first.

“We won 72,783 leu [Romanian currency], which was equivalent of about 18 years of salary,” Mandel told Planet Money in 2016.

Mandel used his winnings to escape Romania with his wife and kids, eventually settling in Australia. And that’s where he perfected his plan. Using a roomful of printers and a computer program he wrote, Mandel figured out the logistics of buying every single number combination in a given lottery, thus guaranteeing a win. All he needed was enough money to buy all the tickets. So he found more and more investors, to whom he promised a portion of the winnings.

It worked. Spectacularly. He and his investors won the lottery. Then they won it again. And again. Mandel won 12 times throughout the 1980s, according to an interview he did with 1990s TV show “How’d They Do That?”

Eventually, Australia made so many changes to its lottery laws that he could no longer run his business.

But 13 wins wasn’t lucky enough for Mandel, so he set his sights on America and the Virginia lottery. It became his target because, back then, Virginia boasted fewer number combinations than other lotteries. (The odds of winning Wednesday’s Powerball are more than one in 292 million.) Mandel concluded it would only cost $7.1 million to buy every ticket. Plus, you could legally print and fill out your ticket at home.

Mandel gathered funds from thousands of investors and then waited for a pot big enough to justify his daring plan. In February 1992, he finally got it. The Virginia lottery had swelled to $27 million — a huge amount at that time — giving Mandel three days to buy every number combination. From Australia.

First, he fired up 30 computers and 12 laser printers to produce the tickets. “We used something like 20 or 30 tons of paper,” he told “How’d They Do That.” Then he dropped $60,000 to ship them to Virginia. From there, a businessman named Anithalee Alex Jr., working for Mandel, organized dozens of accountants to go to gas stations and grocery stores in the Norfolk region to make bulk buys using cashier’s checks.

Everything went smoothly for two days. But, hours before the purchasing window closed, one of the stores stopped selling to them. In the end, they had only paid for somewhere between 5 and 6 million tickets. The foolproof plan wasn’t so foolproof, and while winning was likely, it still came down to luck.

“We had five guys looking through the boxes for the ticket,” Alex said. “When the $27 million ticket came up, everybody was six feet off the ground.”

Not only did they win the jackpot, but according to the website the Hustle, they also scored another million by getting hundreds of minor prizes.

Both the Australian Securities Commission and the FBI investigated. And the Virginia Lottery tried to stop the payout for a little while. But it was all perfectly legal then, though it isn’t now. The lottery director, Ken Thorson, complained about the scheme, telling The Washington Post, “We might remember Thomas Jefferson’s view of a lottery, that it is an opportunity for the common man to spend a small sum for the possibility of a higher prize.”

Speaking of small sums, here’s the thing: Mandel’s 2,500 investors didn’t get rich. After taxes — and paying dozens of people needed to pull off the plan — the group saw about a $1,400 return on their $4,000 investment, according to the Hustle. Conveniently, Mandel paid himself a $1.7 million consultant’s fee.

Ever the international moneymaker, Mandel tried to start his own lottery in Gibraltar in 1993, and later spent some time in an Israeli prison for fraud. But in 2016, when Planet Money spoke to him, he was living in a beach house in the South Pacific — far from the Powerball frenzies that grip America every time a jackpot swells to record amounts.

Read more Retropolis:

He won Powerball’s $314 million jackpot. It ruined his life.

The president was furious over a comedy skit. And it wasn’t Trump fuming about SNL.

John Lennon and Yoko Ono shared their bed with the world 50 years ago — a plea for peace

Weird weather saved America three times

The first black female White House reporter had to pawn her watch every week just to eat