Just as boiling the right amount of pasta for din-din is literally impossible, so too is knowing how much Halloween candy one needs to buy for trick-or-treaters. Or at least, it was impossible to know, because there’s now a “candy calculator” to help people solve that latter problem. Although it does require something far scarier than any trip through a haunted house: doing math.

Popular Mechanics picked up on the Halloween candy calculator, which is really more of a formula. Shipt, a grocery and pharmacy delivery service, and Mars Wrigley, the confectionary behind Skittles, Snickers, etc. came up with the calculator. Which, yes, may very well be a bit biased toward overstocking on delicious Halloween treats. Although we haven’t used the calculator IRL yet, so maybe not.

A basket of Halloween candy next to a jack-o'-lantern, representing a Halloween candy calculator.

The White House

Regardless of its accuracy, the Halloween candy formula, which people can check out here, ostensibly seems useful. The formula only calls for simple addition, multiplication, and division, and folks don’t even need to worry about deploying PEMDAS. (By the way, PEMDAS could be a new candy bar, maybe? But you’d have to eat it in the right order!)

As for the actual calculation, the formula is: (T*K*G) + (D*F*S) = total candy pieces a person needs, where T represents the time in hours one plans to leave the lights on; K, the estimated number of kids per hour who ring the doorbell; G, the “generosity factor,” or how many pieces will go to each trick-or-treater; D, the number of days between the initial candy purchase and Halloween; F, the number of family members in the household; and S, the “sneaky factor, which represents the average number of candy pieces household members will eat per day. One then divides that “total candy pieces” number by 30, as there are, on average, 30 pieces of fun-size candy per bag.

Error occurred!

For example, if somebody plans on buying candy on October 29 (D=3), expects to leave the lights on for two hours per night (T=2), will have roughly two trick-or-treaters visit per hour (K=2), will give each trick-or-treater three pieces of candy (G=3), has four—sneaky yet considerate—family members (F=4)(S=1), then that person should buy…about eight tenths of a bag of candy. Or round up to a single bag, as the folks at Vons may have an issue with fractional purchases.

There are caveats to the Halloween candy formula, of course. For example, the calculator’s creators say that if it rains, the final number of bags should be divided by 1.5. Or, conversely, you can check out Shipt’s map of the most popular candy types in each state (above). If you buy that type, it’ll probably move no matter what the weather’s like. Whether that movement be among sneaky F’s or trick-or-treating K’s.