Problem:
A ladder is to be carried down a hallway p feet wide. Unfortunately at the
end of the hallway there is a right-angled turn into a hallway q feet wide.
What is the length of the longest ladder that can be carried horizontally
around the corner?

Visualization:

Above you can see a crude picture of the situation. The red bar represents
the ladder to be carried. Click on Play to see the animation. You can
change the values of p and q as well as the length of the ladder.

Now let's solve the problem. First instead of trying to find the maximal length
let's see how to determine if a ladder of a given length l can be carried
safely around the corner.

On the picture above we can see the critical moment. The ladder is represented
by a red line segment AB of length l. It is easy to find the coordinates
of B in terms of a:
.
So the equation
of a line passing through AB is

The most important point on the picture is R. Its coordinates are (p,q).
The ladder touches R if

Otherwise we have

So if

then we can carry the ladder without getting stuck if and only if

If we find the minimum of
on (0,l) and test if it is greater or equal
zero then we are done.
Now:

The second derivative tells us that
is concave up, so its critical value
is a minimum. Let's solve

for a by multiplying both sides by
:

If we have the values of p, q, l given, we can compute a0 and
see if
.

If
,
then our ladder has the biggest possible length.
So in order to find the length of the longest ladder plug a0 expressed in
terms of p, q, l into