TITLE: Project Euler Question 222
QUESTION [7 upvotes]: Would I be wrong to assume that the solution to this problem:

What is the length of the shortest pipe, of internal radius 50mm, that can fully contain 21 balls of radii 30mm, 31mm, ..., 50mm?

...involves stacking the balls, from largest to smallest, with each ball resting against the last on alternate sides of the pipe? Like so:

Thus reducing this to a relatively straightforward geometry problem. The reason I ask is because my solution to this arrangement -- calculated in two different ways -- doesn't pass; so I guess there's some kind of subtlety that I'm missing (i.e., making false assumptions!).

REPLY [3 votes]: In connection to my previous post, I suggest the following as a better configuration.
Continuing from the previous post, we have
$X_{v,u} =$ Extra length gained when stacking $S_v$ onto $S_u$
$= (T) – √[200(T) – 100^2]$; where $T = v + u$ = the sum of the two radii
$L = 2∑R_k–∑X_i$ (where $R_k$ is the radius of the $k$-th sphere and $i = 1, 2, …,20$.)
Then, $Min(L) = Min(2∑R_k –∑X_i)$
$= 2∑R_k– Max(∑X_i ) = 2∑R_k– Y$ (say)
Thus, in order to find Min(L), it is sufficient to find the largest of Y’s from different configurations.
Before we go on, we diverse a bit to point out that
$F(T) = T – √(200T–100^2)$ is a decreasing function in the interval $[61, 99]$.
[The proof, which involves the showing of $F’(T) < 0$, is rather strict forward and is omitted.]
The decreasing function provides the following hints for the best stacking method:-

“Putting two smallest spheres adjacent to each other will give a largest gain in X.” ----(*)
“Putting two largest spheres adjacent to each other will give a shortest gain in X.” ----(#)

Let’s first take a look at the simplified situation of having only 3 spheres ($S_{30}$, $S_{49}$, and $S_{50}$)
We need to consider the following 3 cases only:-
The largest in the middle-----$Y(S_{30}, S_{50}, S_{49}) = 2.54538371$
The medium in the middle---$Y(S_{30}, S_{49}, S_{50}) = 2.8473196$
The smallest in the middle---$Y(S_{50}, S_{30}, S_{49}) = 5.38260202$
From this, our observation is
“Putting the smallest between the two largest will get a better gain in length.” ----(@)
This can also be verified mathematically as:-
$Y(S_{50}, S_{30}, S_{49}) – Y$(some other combinations)
$= Y(S_{50}, S_{30}, S_{49}) – Y(S_{30}, S_{49}, S_{50})$; [as an example]
$= (X_{49,30}+ X_{30,50}) – [X_{50,49} + X_{49,30}]$
$= X_{30,50} – X_{50,49}$
$= [80–√[200(80)–100^2] – [99–√[200(99)–100^2]$
$ > 0$ (since $F(T)$ is a decreasing function.)
Next, consider the case of having only 4 spheres (2 largest ($S_{50}$ & $S_{49})$ + 2 smallest $(S_{30}$ & $S_{31})$.
Case-1. Placing the largest two adjacent to each other
----$Y(S_{30} + S_{49} + S_{50} + S_{31}) = … = 5.10724084 =$ length gained is too small
----$Y(S_{50} + S_{49} + S_{31} + S_{30}) = … = 16.6412261$
Case-2. Pair up (One small + one large)
--- $Y(S_{31} + S_{49} + S_{30} + S_{50}) = … = 7.92293509 = $length gained is too small 
--- $Y(S_{30} + S_{49} + S_{50} + S_{31}) = … =$ length gained is too small (as case1-(i)) 
Case-3. Placing the smallest two adjacent to each other
--- $Y(S_{50} + S_{31} + S_{30} + S_{49}) = … = 19.1980326$
--- $Y(S_{50} + S_{30} + S_{31} + S_{49}) = … = 19.176509$
--- $Y(S_{50} + S_{49} + S_{31} + S_{30}) = … = 16.xxxxx$ (as case1-(ii))
This shows that the $S_{50} + S_{31} + S_{30} + S_{49}$ combination has a better usage of the pipe. In fact, all these figures further verified the correctness of (*), (#) and also (@). And they also give us another hint:-
“The largest two spheres should be placed at the far ends (one in each end) of the pipe.” ----(%)
Summing these up, I propose the following configuration:-
$(S_{50}+S_{48}+ … +S_{40}+S_{38}+S_{36}+ … +S_{32})+S_{30}+[S_{31}+S_{33}+ … +S_{47}+S_{49}]$
The corresponding extra length gained $= 89.06688385$
$Min(L) = 1590.933116$, [a figure far less than the $S_{50}+S_{30}+S_{49}+S_{31}+ ...$ configuration.]