Fiddler on the Proof: October 17, 2025

David Ding

October 17, 2025

Can You Reach the Edge of the Square?

You start at the center of the unit square and then pick a random direction to move in, with all directions being equally likely. You move along this chosen direction until you reach a point on the perimeter of the unit square.

On average, how far can you expect to have traveled?

Extra Credit: Let’s raise the stakes by a dimension. Now, you start at the center of a unit cube. Again, you pick a random direction to move in, with all directions being equally likely. You move along this direction until you reach a point on the surface of the unit cube.

On average, how far can you expect to have traveled?

Solution

Both the original puzzle and the extra credit blend geometry with probability. The geometry asks you for an expression of the line's distance from the center to the perimeter point of intersection for any given direction, and the probability part asks you to take the average of those distances by summing them up weighted by the probability of the corresponding directions. Well, by "summing" I meant "integrating", as the directions are continous and not discrete here, but you get what I mean.

So, with that in mind, let's tackle the square case. First, let's visualize the problem and define some variables. See below:

square diagram

So in our unit square, let \(\theta\) represent the angle between the x-axis and the line that connects the center to the perimeter. Hence, in this case, picking a \(\theta\) is equivalent to picking a direction. Also, note the symmetry of the problem: we only need to consider \(0 \leq \theta \leq \frac{\pi}{4}\) because this is the same exercise repeated on the seven other half quadrants of the unit square.

With that in mind, the geometry part becomes this: let \(d(\theta)\) be the length of the line for any given theta, \(0 \leq \theta \leq \frac{\pi}{4}\). Using trigometry and noting that a right-angle triangle is formed from the x-axis, line, and the side of the square from where x-axis meets to where the line meets, we have:

\begin{align} \cos{\theta} &= \frac{1/2}{d} \\ \\ \cos{\theta} &= \frac{1}{2d} \\ \\ 2d &= \frac{1}{\cos{\theta}} \\ \\ d(\theta) &= \frac{1}{2\cos{\theta}} \\ \end{align}

There you have it. Now onto the probability part. In order to find the average distance traveled, i.e. the expected length of the line for any given direction, we must integrate over the angles considered and divide by the probability of those angles occuring given the fact that all directions are equally likely to be chosen. Now, this is a very important concept to grasp, and I don't want to not mention it for the square case just because it looks "obvious" that all angles \(\theta\) are equally likely to be chosen here, because this can lull us into missing key insights when we get to the cube case. So, rigorously speaking, the expected distance resembles the following:

\begin{align} E[d] &= \frac{\int_{0}^{\pi/4} d(\theta) \, \mathrm{d}\theta}{\int_{0}^{\pi/4}\, \mathrm{d}\theta} \\ \\ &= \frac{\int_{0}^{\pi/4} \frac{1}{2\cos{\theta}} \, \mathrm{d}\theta}{\int_{0}^{\pi/4}\, \mathrm{d}\theta} \\ \\ &= \frac{2}{\pi} \int_{0}^{\pi/4} \frac{1}{\cos{\theta}}\, \mathrm{d}\theta \\ \end{align}

Now, we use the most powerful integration tool of all time: the look-up table, and obtain that:

\begin{align} & \int \frac{1}{\cos{\theta}}\, \mathrm{d}\theta \\ \\ &= \log|\tan{\theta} + \sec{\theta}| + C \end{align}

Okay okay let's derive it for those that are curious.

First, consider the derivative of \(\sec(x)\):

\begin{align} \sec'{x} &= \left(\frac{1}{\cos{x}}\right)' \\ \\ &= \left(\frac{\sin{x}}{\cos^2{x}}\right) \\ \\ &= \tan{x} \sec{x} \\ \end{align}

Also, using the quotient rule and remember \(\tan = \sin/\cos\), we have:

\begin{align} \tan'{x} &= \sec^2{x} \\ \end{align}

Therefore:

\begin{align} (\tan{x} + \sec{x})' &= \sec^2{x} + \tan{x} \sec{x} \\ \\ &= \sec{x}(\sec{x} + \tan{x})\\ \end{align}

How will this help? Well, watch:

\begin{align} & \int \frac{1}{\cos{\theta}}\, \mathrm{d}\theta \\ \\ =& \int \sec{\theta}\, \mathrm{d}\theta \\ \\ =& \int \frac{\sec{\theta}(\sec{\theta} + \tan{\theta})}{(\sec{\theta} + \tan{\theta})}\, \mathrm{d}\theta \\ \end{align}

Now do u-sub and let \(u = \sec{\theta} + \tan{\theta}\). We know that \(\mathrm{d}u = \sec{\theta}(\sec{\theta} + \tan{\theta})\, \mathrm{d}\theta\), and so:

\begin{align} & \int \frac{1}{\cos{\theta}}\, \mathrm{d}\theta \\ \\ =& \int \frac{\sec{\theta}(\sec{\theta} + \tan{\theta})}{(\sec{\theta} + \tan{\theta})}\, \mathrm{d}\theta \\ \\ =& \int \frac{1}{u} \, \mathrm{d}u \\ \\ =& \log |u| + C \\ \\ =& \boxed{\log|\tan{\theta} + \sec{\theta}| + C} \end{align}

Therefore, we have:

\begin{align} E[d] &= \frac{2}{\pi} \int_{0}^{\pi/4} \frac{1}{\cos{\theta}}\, \mathrm{d}\theta \\ \\ &= \frac{2}{\pi} \left(\log\Bigl|\tan{\frac{\pi}{4}} + \sec{\frac{\pi}{4}}\Bigr| - \log|\tan{0} + \sec{0}|\right) \\ \\ &= \frac{2}{\pi} \left(\log(1 + \sqrt{2}) - \log(1)\right) \\ \\ &= \boxed{\frac{2}{\pi} \log(1 + \sqrt{2})} \\ \\ &\approx 0.5611 \end{align}

Sanity check: \(\text{min val: } 0.5 \leq 0.5611 \leq \text{max val: } \sqrt{2}/2 \approx 0.7071\).

Below is an animation of the changes of the distance as we uniformly sweep the direction in \(\theta\) for \(0 \leq \theta \leq \pi/4\).

Extra Credit

Now, we add a whole new dimension, and with it, some careful treading is needed. For the geometry part, it's now more important than ever that we visualize the problem, and with it, some new angle definitions. Please see below:

cube diagram

Now we have two angles that are needed to define the direction we are traveling for the unit cube case. First, the \(\theta\) is pretty much the same as what we had for the square case: the angle that dictates our direction in the x-y plane. However, now we also introduce a new angle, the elevation angle \(\phi\), where it is the angle between the x-y plane and the line that dictates our direction in our new third dimension.

Again, by symmetry, we can just consider the case where \(0 \leq \theta \leq \pi/4\) and \(0 \leq \phi \leq \pi/4\), and of course, when both \(\theta\) and \(\phi\) are equal to \(\pi/4\), we are going from the center of the unit cube all the way to one of its vertices, hence at this point the distance is maximized to be \(\sqrt{3}/2 \approx 0.866\). So our answer should lie in between 0.5 and 0.866.

For the geometry part, now our distance formula should depend on both \(\theta\) and \(\phi\). For the derivation, let's form the line in two steps. First, pick a \(\theta\) for \(0 \leq \theta \leq \pi/4\) while staying on the x-y plane. Call that length \(l\). Using trigonometry, we have:

\begin{align} \cos{\theta} &= \frac{1/2}{l} \\ \\ \cos{\theta} &= \frac{1}{2l} \\ \\ 2l &= \frac{1}{\cos{\theta}} \\ \\ l &= \frac{1}{2\cos{\theta}} \\ \end{align}

Okay, so same as the square case, as expected. Now, we pick the \(\phi\) for \(0 \leq \phi \leq \pi/4\) as our elevation part of the direction. Now we use the same trig thinking as before except our base is now \(l\) instead of the x-axis, so the distance \(d\) becomes:

\begin{align} \cos{\phi} &= \frac{l}{d} \\ \\ d &= \frac{l}{\cos{\phi}} \\ \\ d(\theta, \phi) &= \boxed{\frac{1}{2\cos{\theta}\cos{\phi}}} \\ \end{align}

Now, we bring in the probability, and with it, some critical thinking. At first, you might be tempted to equate this cube case to the square case, where instead of uniformly choosing the \(\theta\) only, now you get to uniformly choose the new angle \(\phi\) as well. Spoiler alert: this is incorrect thinking. Why? Let me explain.

Ask yourself: what time zone are you in right now? This seems like a straightforward question, but only because you are sufficiently away from the poles of the Earth! When you are near the poles, the lines of longitude are so squished together that time zones lose their meanings. For example, scientists in Antarctica don't use the "local" time zone on that continent because it's meaningless: they are too close to the South Pole. Instead, they pick whatever time zone they are familar with, most likely the time zone of their home country. How is this related to our problem? Well, just like how the time zones cover smaller and smaller physical distances the further away from the equator, once the elevation angle \(\phi\) is selected, the angle \(\theta\) covers less distance for the same unit amount as compared to when \(\phi\) is close to 0 degrees. This would throw off our expected distance since from the problem it is given that all directions are equally likely to be chosen.

Therefore, in order to cover this case, we have to ensure that for every elevation, we are picking the distances equally likely. This can be achieved by "scaling" the entire distance covered by all \(\theta\) choices for a given elevation \(\phi\) by a special factor as compared to the base case where we are just on the x-y plane. Well, recall that the circumference of a circle is just \(r \theta\), where \(r\) is the radius of the circle. On a sphere, for a given \(\phi\), the radius of the lattitude circle at that elevation is \((R\cos{\phi})\), where \(R\) is the radius of the sphere (you can verify this using trigonometry). Now, since the sphere's radius \(R\) is fixed, the relative circumference scales as \((\cos{\phi})\) (since circumference is proportional to radius). Hence, our probability weighting factor is \((\cos{\phi})\).

So our unit "direction" is actually:

\begin{align} \boxed{\cos{\phi}\,\mathrm{d}\theta\,\mathrm{d}\phi} \end{align}

Here, the angle of elevation \(\phi\) is still chosen uniformly. (But will this be the case in 4D, 5D? Feel free to find out on your own!).

So, putting everything together, we finally get for the expected distance:

\begin{align} E[d] &= \frac{\int_{0}^{\pi/4}\int_{0}^{\pi/4} d(\theta, \phi)\cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi}{\int_{0}^{\pi/4}\int_{0}^{\pi/4} \cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi} \\ \end{align}

Let's take a breather here. This is no different than the square case, except we have to integrate over two angles instead of one and integrate over our proper definition of what constitutes a "unit direction" for the 3D case, which we just analyzed, and divide the same as well.

And now let's continue:

\begin{align} E[d] &= \frac{\int_{0}^{\pi/4}\int_{0}^{\pi/4} d(\theta, \phi)\cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi}{\int_{0}^{\pi/4}\int_{0}^{\pi/4} \cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi} \\ \\ &= \frac{\int_{0}^{\pi/4}\int_{0}^{\pi/4} \frac{1}{2\cos{\theta}\cos{\phi}}\cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi}{\int_{0}^{\pi/4}\int_{0}^{\pi/4} \cos{\phi} \, \mathrm{d}\theta\, \mathrm{d}\phi} \\ \\ &= \frac{\int_{0}^{\pi/4}\, \mathrm{d}\phi\int_{0}^{\pi/4} \frac{1}{2\cos{\theta}} \, \mathrm{d}\theta}{\int_{0}^{\pi/4}\, \mathrm{d}\theta\int_{0}^{\pi/4} \cos{\phi} \, \mathrm{d}\phi}\\ \end{align}

Again, let's take a breather here. The double integrals look intimidating, but it's really not. First, for the numerator we can use our results from the square case, and secondly, the integrands for both the numerator and the denomerator are separable, and so we did separate them just then. Now let's continue:

\begin{align} E[d] &= \frac{\int_{0}^{\pi/4}\, \mathrm{d}\phi\int_{0}^{\pi/4} \frac{1}{2\cos{\theta}} \, \mathrm{d}\theta}{\int_{0}^{\pi/4}\, \mathrm{d}\theta\int_{0}^{\pi/4} \cos{\phi} \, \mathrm{d}\phi}\\ \\ &= \frac{\left(\frac{\pi}{4}\right)\left(\frac{\log(1 + \sqrt{2})}{2}\right)}{\left(\frac{\pi}{4}\right)\sin{\left(\pi/4\right)}} \\ \\ E[d] &= \boxed{\frac{\log(1 + \sqrt{2})}{\sqrt{2}}} \\ \\ &\approx 0.6232 \end{align}

Lot's of things cancelled out in our derivation, and our exact value lies in between the theoretical minimum and maximum, so we ended up with a really elegant solution!

Below is an animation I made in MATLAB that shows the sweeps of \(\theta\) from 0 to 45 degrees over different elevation angles \(\phi\) at a constant frame rate per arc-length swept. This is to show how at higher elevation angle \(\phi\), each arc-length represented by unit \(\theta\) is physically smaller, since at higher values of \(\phi\), the sweep finishes faster given the same frame rate per arc-length. Therefore, if we omit the \(\cos{\phi}\) factor, the distances for higher elevations will be "overcounted" if we simply chose \(\theta\) uniformly regardless of \(\phi\). And since higher the elevation, the greater the distance from our geometric result, this would incorrectly overshoot the expected distance value.