The Riddler is a weekly puzzle series from Oliver Roeder at FiveThirtyEight that usually involves some combination of math, logic and probability.

This weeks FiveThirtyEight Riddler, Can You Solve This Elevator Puzzle was:

In a building’s lobby, some number (

N) of people get on an elevator that goes to some number (M) of floors. There may be more people than floors, or more floors than people. Each person is equally likely to choose any floor, independently of one another. When a floor button is pushed, it will light up.

What is the expected number of lit buttons when the elevator begins its ascent?

Similar to last week, I wanted to simulate the problem before jumping into the math. This’ll also help later to check my work.

While certainly not elegant, all I did here was create 10 arrays of 0’s of size 5 to 50 by 5. Array size represents total number of floors (M in context of the problem). From there, I randomly generated an integer from 1 to M, and changed the number at that array index to 1 – representing a pushed button. Repeated the random number generation N times to represent different passenger sizes (all the way until N = M). Finally I re-did this for each array 100 times at a given M/N and then averaged the array sum (representing buttons pushed. Again, not elegant, but it gave me a quick representation of what the data should look like.

We’ll be using f_{N} to represent expected value of pressed elevator buttons throughout this post.

Initially let’s deal with a situation where M (floors) = 5. The expected pressed buttons for the first passenger (f_{1}) is fairly straightforward:

f

_{1}= 1

Since the next passenger is equally likely to select any of the 5 buttons, the elevator configurations after he presses a button are:

O | O | X | X | O |

Where he selects a different button from passenger 1 or

O | O | X | O | O |

selects the same button as passenger 1. Given that he has a 1/5 chance of selecting the same button as passenger 1, we compute f_{2} to be:

f

_{2}= 1 [number of buttons already pushed] + (1)*((M-1)/M) [probability of selecting unique button]@ M=5 this evaluates to:

f

_{2}= 1 + (4/5)/5 = 9/5 = 1.8

The f_{2} equation can be more generally written as:

f

_{2}= f_{1}+ (M-f_{1})/Mor

f

_{N}= f_{N-1}+ (M-f_{N-1})/M

Now that we have an equation, let’s test it against some of the data we generated earlier.

So our equation (dashed blue line) fits the simulated data (coral solid line) fairly well. Now we can start trying to simplify our equation. Let’s start by evaluating f_{N} in N = 1:4.

Let c = (M-1)/M.

f

_{1}= 1f

_{2}= 1+f_{N}-1*c = 1 + f_{1}*c = 1 + cf

_{3}= 1 + f_{N}-1*c = 1 + f_{2}*c = 1 + (1+c)*c = 1 + c + c^{2}f

_{3}= 1 + f_{N}-1*c = 1 + f_{3}*c = 1 + (1 + c + c^{2})*c = 1 + c + c^{2}+ c^{3}

The trend is clear. It’s a geometric series that can be written as:

We go back and plug in c = (M-1)/M and find the solution to f_{N}

f

_{N}= ((M-1/M)^N -1)/((M-1)-1) =M – M*((M-1)/M)^N

~~Since we’re concerned with the proportion of pushed buttons, and fN gives you the expected number of pushed buttons, simply divide out M and get the final solution~~

~~1 – ((M-1)/M)~~

**EDIT (05.13.2016): **I misread the question, and we want expected number of buttons pushed, not expected proportion of buttons pushed. Therefore the final answer is:

M – M*((M-1)/M)^N