# Candy Challenge

The other day I came across an interesting task that seemed to me worthy of the audience of this resource. Its condition is as follows:

It is worth saying that the condition was not pulled out of the Internet or peeped at any resource of entertaining tasks, but came from one very good friend, who, by his position, is an engineer for organizing and managing production at a well-known confectionery factory. That is, the problem has a very real origin, and its solution is of practical use.

I invited readers to solve the problem on their own and I must say that they coped with it better than me. In my decision, I made the wrong assumption.

Let us agree to indicate in capital letters the parameters for the box and in small letters for the candies.

Let and - respectively, the net mass of the box and its permissible deviation, such that in percent of cases it does not go beyond .

Let and - accordingly, the mass of candy and its tolerance, such that in percent of cases it does not go beyond .

The number of candies in the box .

The normal distribution is described by the Gauss function:

, where is the mathematical expectation, is the standard deviation, the square of which is called the dispersion.

In the case of sweets , a , therefore:

In the case of a box of chocolates , a :

The probability that the mass of candy does not go beyond is:

The probability that the box will not go beyond the net is:

The figure below illustrates well all of the above:

Find the probability for candy:

, where is the distribution function, and is the error function.

So for candy:

Similarly for the box:

From the central limit theorem it follows that if there are independent random variables:

, then their sum:

will have parameters:

In relation to our situation, we have:

I mistakenly calculated that the total probability for a box is equal to the product of the probabilities for individual candies. In other words:

from where:

The resulting system of equations:

Having decided it regarding :

, deduced:

, where is the inverse error function and found quite specific numbers:

I substantiated this decision as follows: it is necessary that the mass of candy does not go beyond 25.8333 ± 3.2212 in 99.13% of cases (1 in 115). And although such an answer is not contradictory, the truth is that it is only the correct answer. Since with such a smaller standard deviation, we do not need to drop anything, which many readers have hinted to me for a long time and were right.

As without her. Constructed a check in matlaba. In short, we create 1,000,000 candies with the found parameters according to the normal law. In a random (equiprobable) way, from them we form 1,000,000 groups (count the boxes) of 12 pieces each. We check the number of such groups that did not go beyond 310 ± 7 and divide by the total, thus obtaining the very probability for the box. And so 1000 times.

The result was such a beautiful schedule:

I dare to assume that 1 million boxes are few and if we direct their number to infinity, our probabilities will be exactly 90%.

Much more interesting is the case when the candy filling machine has a constant standard deviation that is greater than 1.2285 and you need to find the very boundaries above which, for a given candy, you need to drop the candy to satisfy the same conditions. This is a much more difficult task, which I may devote to another article.

**“Find the maximum allowable deviation of the candy mass during its manufacture, so that the net box consisting of 12 pieces of them does not go beyond 310 ± 7 grams in 90% of cases. The law of distribution is considered normal. ”**It is worth saying that the condition was not pulled out of the Internet or peeped at any resource of entertaining tasks, but came from one very good friend, who, by his position, is an engineer for organizing and managing production at a well-known confectionery factory. That is, the problem has a very real origin, and its solution is of practical use.

I invited readers to solve the problem on their own and I must say that they coped with it better than me. In my decision, I made the wrong assumption.

# 1. Conventions

Let us agree to indicate in capital letters the parameters for the box and in small letters for the candies.

Let and - respectively, the net mass of the box and its permissible deviation, such that in percent of cases it does not go beyond .

Let and - accordingly, the mass of candy and its tolerance, such that in percent of cases it does not go beyond .

The number of candies in the box .

# 2. Normal distribution

The normal distribution is described by the Gauss function:

, where is the mathematical expectation, is the standard deviation, the square of which is called the dispersion.

In the case of sweets , a , therefore:

In the case of a box of chocolates , a :

The probability that the mass of candy does not go beyond is:

The probability that the box will not go beyond the net is:

The figure below illustrates well all of the above:

Find the probability for candy:

, where is the distribution function, and is the error function.

So for candy:

Similarly for the box:

# 3. Central limit theorem

From the central limit theorem it follows that if there are independent random variables:

, then their sum:

will have parameters:

In relation to our situation, we have:

# 4. Probabilities and my mistake

I mistakenly calculated that the total probability for a box is equal to the product of the probabilities for individual candies. In other words:

from where:

The resulting system of equations:

Having decided it regarding :

, deduced:

, where is the inverse error function and found quite specific numbers:

I substantiated this decision as follows: it is necessary that the mass of candy does not go beyond 25.8333 ± 3.2212 in 99.13% of cases (1 in 115). And although such an answer is not contradictory, the truth is that it is only the correct answer. Since with such a smaller standard deviation, we do not need to drop anything, which many readers have hinted to me for a long time and were right.

# 5. Verification

As without her. Constructed a check in matlaba. In short, we create 1,000,000 candies with the found parameters according to the normal law. In a random (equiprobable) way, from them we form 1,000,000 groups (count the boxes) of 12 pieces each. We check the number of such groups that did not go beyond 310 ± 7 and divide by the total, thus obtaining the very probability for the box. And so 1000 times.

**The code**

```
% Number of candys
nC = 1000000 ;
% Mass deviation of a single candy
mC = normrnd ( m , s , 1 , nC ) ;
% Number of candys in the box
n = 12 ;
% Number of boxes
nB = 1000000 ;
% Number of experiments
nE = 1000;
pB = zeros ( 1, nE );
for k = 1 : nE
% Random index of n candys
i = random ( 'unid' , nC , nB, n ) ;
% The mass of each boxes
j = 1 : nB ;
mB = sum ( mC ( i ( j , : ) ) , 2 )' ;
% Mask boxes that out of range
mask = ( mB < M + dM ) .* ( mB > M - dM );
% Probability of out of the range
pB ( k ) = sum ( mask ) / nB;
end
```

The result was such a beautiful schedule:

I dare to assume that 1 million boxes are few and if we direct their number to infinity, our probabilities will be exactly 90%.

Much more interesting is the case when the candy filling machine has a constant standard deviation that is greater than 1.2285 and you need to find the very boundaries above which, for a given candy, you need to drop the candy to satisfy the same conditions. This is a much more difficult task, which I may devote to another article.