Counting cards at the casino may pay out more than counting candies in a jar, although a close goodie guesstimate could still win you a sweet prize at the county fair. But just how do you estimate a figure for all the candies you can't see in the inner part of the jar, especially if they vary in size?

Researchers at New York University (N.Y.U.) may finally have the answer to this classic mathematical puzzle. Using transparent, fluorescently labeled oil droplets in water, the physicists looked beyond the outermost layer of spherical particles within the container—the only layer visible when looking from the outside—to spy on the geometric behavior inside. The resulting formula, which simply requires the sizes of the particles and the container as inputs, could make fair officials nervous.

"You give us the distribution of sizes, and we can tell you how it will pack," says Jasna Brujic, who led the project.

Brujic and her colleagues at N.Y.U.'s Center for Soft Matter Research used their 3-D oil–water model to determine that bigger particles, when packed in a container holding a combination of sizes, made more contacts with neighbors than smaller ones. That much was intuitive, Brujic says, given their larger surface area. What was more surprising was that the average number of contacts for a particle within any mixed container was always six. This number, she explains, maintains mechanical stability.

The researchers also knew from previous work that randomly packed identical spheres fill up about 64 percent of the volume in a given container. But Brujic's team was the first to describe how that proportion, or density, grows when the spheres vary in size; smaller ones can fill in voids that larger ones can't.

So, what should a contestant do if he or she wants to guess at the number of candies in a jar, but lacks the complicated formula and a handy computer? "First, estimate the size of the jar," instructs Brujic. "Then look to see if all the candies are the same size. If they are, take 64 percent of that volume and divide it by the size of the candy to get the total number that would randomly fit inside. If they aren't equally sized, divide a slightly larger area, around 70 percent, by the average size of the candies."

The team's experiments were all based on spherical particles, so Brujic notes that corrections are needed when generalizing to other shapes. Counting jellybeans would be more complicated than gumballs, for example.

The applications for this finding extend well beyond the fairgrounds—ranging from aiding oil extraction to filling vending machines to creating a paint that dries faster or a pill that is easier to swallow. Brujic, however, returns to the candy model with her big business idea: "If you want to make the most money as a sweet-maker, you could tune the size distribution to get a small density," Brujic says. Or, for the more generous candy-maker, the reverse could work: "If you are a Charlie and the Chocolate Factory manufacturer, you could find a way to fit the most chocolates in your bag." She is considering asking confectioner Mars, Inc., for her next grant.

The study was published in the latest issue of the journal Nature. (Scientific American is part of the Nature Publishing Group.)