TITLE: Expected number of items looked at
QUESTION [6 upvotes]: Currently failing at basic probability…
Given a sequence of items, linear search means looking at each in turn and seeing if it's the one we're looking for. (I.e. in the worst case, it's the last item in the sequence or not in the sequence at all and we'll have to look at all items to find it/determine it's not there.)

How many elements of the input sequence need to be checked on average, assuming that the element being searched for is equally likely to be any element in the array?

Ok, we know two things:

the element we're looking for is in the array
P[the i-th item is the one we're looking for] = $\frac{1}{n}$ for all $i\in\{1,..,n\}$

And I think I'm supposed to get $\frac{n+1}{2}$ or $\frac{n}{2}$ or something thereabout.
Let $X$ be the number of items the linear search algorithm is looking at.
$$\begin{align}
P[X=1] &= \frac{1}{n} \\
P[X=2] &= \left(1-\frac{1}{n}\right)\frac{1}{n} \\
P[X=3] &= \left(1-\frac{1}{n}\right)^2\frac{1}{n} \\
& \ \ \vdots \\
\end{align}$$
So 
$$\begin{align}
\mathbb{E}(X) &= \sum_{i=1}^n i\left(1-\frac{1}{n}\right)^{i-1}\frac{1}{n} \\
&= \frac{1}{n}\sum_{i=1}^n i\left(1-\frac{1}{n}\right)^{i-1} \\
\end{align}$$
That's not something I want to solve myself, so I gave it to Wolfram Alpha and got
$$\left(1-2\left(\frac{n-1}{n}\right)^n\right)n$$
This doesn't look particularly close.
Where did I go wrong?

REPLY [2 votes]: This is a matter of conditionality.
In your calculation, you took $1/n$ to be the probability that the element is in the $i$th position under the condition that it was not in any of the positions before $n$.
If this were the case, then your calculation of the probabilities for $i\geq2$ would be correct.
Unfortunately though, the probabilities don't sum up to one.
Instead, the probability to find the element in the $i$th position is precisely $1/n$ for every $i\in\{1,\dots,n\}$.
The probabilities are given, and there is no need to find them.
The probabilities are unconditional; events for $i<j$ have no bearing on $P[X=j]$.
If you want, you can use this information to find the conditional probabilities.
They are not needed in this problem, but you will find that they are not $1/n$.
There is two alarming things in your calculations, hinting at a problem:

You assumed $P[X=2]=\frac1n$ and the calculated that $P[X=2]=(1-\frac1n)\frac1n$.
The probabilities don't sum up to one although they should.