# Expected number of keys to try

You have n keys on your key ring. One of them unlocks the front door, but you don't know which one. Find the expected number of tries it takes to open the front door by using the method of indicator random variables.

Note: We try keys without repeats since that key will never work if it doesn't work once.

My attempt:

Let X be the number of tries we need to open the front door. We see that $$X = 1 + X_1 + X_2 + X_3 + ... + X_n$$ where $$X_i = 1$$ if the $$i^{th}$$ try doesn't open the door and $$X_i = 0$$ if the try opens the door. We need at least one try to open the door which is why there's a $$1$$ as the first term in the sum.

Using linearity of expectation: $$E[X] = 1 + \sum_i {E[X_i]} = 1 + \sum_i {P(X_i = 1)}$$

Now attempting to find $$P(X_i = 1)$$. Well we've already tried $$i - 1$$ keys at this point, so there are $$n - i + 1$$ keys left. Of those $$n - i + 1$$ keys, $$n - i$$ of them are incorrect. Therefore, the probability that the $$i^{th}$$ try doesn't work is $$\frac{n - i}{n - i + 1}$$.

So, $$E[X] = 1 + \sum_i {E[X_i]} = 1 + \sum_i {P(X_i = 1)} = 1 + \sum_{i = 1}^{n} \frac{n - i}{n - i + 1}$$ I typed this summation into wolframalpha and got some ugly answer with a $$\psi$$ in it, while the answer from the book I'm using gives the answer as $$\frac{(n+1)}{2}$$.

Where am I going wrong? Any advice is appreciated :)

• If we try keys without repeats, then it will take at most $n$ tries to open the door. The book's answer doesn't make sense in this context because $\frac{n(n+1)}2>n$. I may be horribly wrong, or perhaps you're supposed to try keys at random? – doobdood Jun 23 '20 at 20:40
• I don't understand your expression for $X$. In any case, your selection of the order in which to examine the keys amounts to a random permutation of the key and the winning key has an equal chance of appearing anywhere in such a permutation. – lulu Jun 23 '20 at 20:41
• That's the probability on the first try. After that, the probability increases. But that's not important...since it takes you $k$ tries with probability $\frac 1n$ no matter what $k$ is, the answer is $\frac 1n\times \frac {n(n+1)}2=\frac {n+1}2$. – lulu Jun 23 '20 at 20:46
• Your definition of the $X_i$ means that exactly one of the variables $X_i$ is $0$, the rest being $1$, so $X=n$. Your definition of $X$ implies that you actually try every key, continuing to try even after you’ve opened the door. – Brian M. Scott Jun 23 '20 at 20:47
• If you really need to do it via indicator variables, let $Y_i$ be the indicator variable for the event "you test the $i^{th}$ key before you find the right one". Then $E=1+\sum Y_i$ – lulu Jun 23 '20 at 20:49

$$P(\text{1st attempt works}) = \frac{1}{n}$$ $$P(\text{2nd attempt works}) = \frac{n-1}{n} \frac{1}{n-1} = \frac{1}{n}$$ $$P(\text{3rd attempt works}) = \frac{n-1}{n} \frac{n-2}{n-1} \frac{1}{n-2}= \frac{1}{n}$$ $$...$$ $$P(i^{th}\text{ attempt works}) = \frac{1}{n}$$ $$E[X] = (1)\frac{1}{n} + (2)\frac{1}{n} + (3)\frac{1}{n} + (4)\frac{1}{n} + .. + (n)\frac{1}{n} \\ = \frac{1}{n}[1 + 2 + 3 ... + n] \\ = \frac{1}{n}[\frac{n(n+1)}{2}] = \frac{n+1}{2}$$ Where we have used the sum of an arithmetic series to get $$[\frac{n(n+1)}{2}]$$