You're getting all prepped for the trick or treaters on Halloween night, but did you buy enough candy? Will you come up short and have to shut things down early? Or will you be stuck with extra bags of candy that you really don't want to eat?

Well believe it or not, there's now a kind of complicated formula to figure these things out thanks to the folks at, of all places, popularmechanics.com.

Get our free mobile app

Granted, this formula is a bit complicated and for some confusing, but if you do your calculations correct, you should be able to assess the the situation better, and get some piece of mind.

So get ready, here's the formula:

It's T times K times G, plus D, times F, times S, then divide by 75. Feel like your back in 10th grade calculus again? Well you're probably not alone.

Now here's what that all means in more layman's terms.

"T" represents the amount of time, in hours, that you plan on handing out candy. "K" represents the estimated number of kids per hour you think will show up trick or treating. "G" represents the generosity factor, or actually how many pieces of candy you'll be giving to each kid. "D" stands for the number of days you'll have the candy in your house before Halloween, which is meant to factor in the candy you'll actually eat before Halloween. "F" represents the number of family members in your house that might eat some of that candy too, and finally "S" stands for the sneakiness level, or the projected number of pieces that you and your family might consume each day before Halloween.

You then divide it all by 75, because that's the average number of fun size candy bars in each bag you buy.

So in other words, let's say you plan on handing out candy for 3 hours, times that by about 70 kids an hour who'll be stopping at your house, multiply that by the three pieces of candy you'll be giving to each kid, and that all adds up to 630 pieces of candy. Now Halloween isn't here yet, there's still 3 more days to go, and then we multiply that by 4 members of your household, who may each sneak two pieces of that candy a day, which equals 24 pieces of candy. Now add that number to the first number, (630 pieces in all the bags you bought), gives us the magic number of 654 pieces of candy needed. Finally divide that number by 75 and it tells us you'll need about 9 bags of candy to actually get through all the trick or treaters your expecting.

Oh, there is one more thing to consider, and that's bad weather. If there's any rain in the forecast, then divide that number by 1.5. That gives you about a third less candy needed. Right now our Sunday forecast looks just partly cloudy with only a 20% chance of showers, so you may not have to add in that extra step.

Is this a very complicated formula, the answer is yes, but when you break it down it will give you the most accurate candy count possible. Also based on the calculations we've done, and the manufactures suggested retail price of those fun size bags, your candy bill will probably be somewhere in the ballpark of $90 dollars, plus tax for your Halloween candy supply.

LOOK: How Halloween has changed in the past 100 years

Stacker compiled a list of ways that Halloween has changed over the last 100 years, from how we celebrate it on the day to the costumes we wear trick-or-treating. We’ve included events, inventions, and trends that changed the ways that Halloween was celebrated over time. Many of these traditions were phased out over time. But just like fake blood in a carpet, every bit of Halloween’s history left an impression we can see traces of today.

SWEET: Here are the most popular Halloween candies

LOOK: 34 spooky dessert recipes for this Halloween