A coin flipped until two of the most recent three flips are Head. Let $N$ denote the number of flips. Find $E(N)$ A coin for which $P(Head)=0.6$ is successively flipped until two of the most recent three flips are Head. Let $N$ denote the number of flips. Find $E(N)$.
What I did: 
Let X denote the first time a Head appears. Then $E(N)=E[E(N|X)]=\sum_{i=1}^{\infty}E(N|X=i)=\sum_{i=1}^{\infty}NP_{N|X=i}(n)=\sum_{i=1}^{\infty}N\frac{P(N,X=i)}{P_{X}(X=i)}$
Does anyone could help me to continue doing this problem？Thanks!
 A: Let $X$ be the first time that H appears. What happens next?


*

*With probability $p = 0.6$, next is H again; then we are finished.

*With probability $(1 - p) p$, we get T then H; then we are finished.

*With probability $(1 - p)^2$, we get T then T; then it is as if we are starting anew.


So let $T$ denote the first time H appears that will be a second H among three last flips. 
With the above reasoning,
$$
E[T] = p (E[X] + 1) + (1-p) p (E[X] + 2) + (1 - p)^2 (E[T] + 3)
.
$$
If I simplified correctly, which I doubt:
$$
E[T] = E[X] + (1 - p)/2
$$
Finally, 
$$
E[X] = \sum_{n \geq 1} n P[\text{$(n-1) \times$ T followed by H}] = \sum_{n \geq 1} n p (1-p)^{n-1} = -p f'(p)
$$
where $f(x) := \sum_{n \geq 1} (1-x)^n = 1/x - 1$, so $f'(x) = -1/x^2$,
so $E[X] = 1/p$. There is probably a quicker way to find $E[X]$.
A: A different approach, still using conditional expectation ...
Let $p=P(H)$ and $q=P(T)$. Condition on the result of the first two flips:
\begin{eqnarray*}
E(N) &=& 2 + E(N\vert HH)P(HH) + E(N\vert HT)P(HT) + E(N\vert TH)P(TH) + E(N\vert TT)P(TT) \\
&=&2 + p^2 E(N\vert HH) + pqE(N\vert HT) + pqE(N\vert TH) + q^2E(N\vert TT)
\end{eqnarray*}
For easier notation, let $E=E(N)$ and also $E_{HH}=E(N\mid HH)$, and so on. Then we have:
$$E = 2 + p^2 E_{HH} + pqE_{HT} + pqE_{TH} + q^2E_{TT}.$$
Next we have:
\begin{eqnarray*}
E_{HH} &=& 0 \qquad\text{since we have two heads} \\
&& \\
E_{TT} &=& E \qquad\text{since after two tails it's like starting over} \\
&& \\
E_{HT} &=& 1 + (0\cdot P(H) + E_{TT}\cdot P(T)) \qquad\text{(next H gives HTH and we are done)} \\
&=& 1 + qE \\
&& \\
E_{TH} &=& 1 + (E_{HH}\cdot P(H) + E_{HT}\cdot P(T)) \\
&=& 1 + q(1 + qE) \\
&=& 1 + q + q^2E. \\
\end{eqnarray*}
Substitute these into the above equation, giving
\begin{eqnarray*}
E &=& 2 + pq(1+qE) + pq(1+q+q^2E) + q^2E \\
(1-pq^2 - pq^3-q^2)E &=& 2 + 2pq + pq^2 \\
E &=& \dfrac{2 + 2pq + pq^2}{1-pq^2 - pq^3-q^2}. \\
\end{eqnarray*}
Substituting $1-p$ for $q$, this simplifies to:
$$E(N) = \dfrac{1+2p-p^2}{p^2(2-p)}.$$
For $p=0.6$ we have $E(N) \approx 3.65$.
