A fair coin is flipped until we get at least one head and one tail. What is the expected value of the number of flips?
I. solution. The number of necessary throws is exactly $n$ if the first $n-1$ throws are heads and the $n$-th is tails - the probability of this is $\left(\frac{1}{2}\right)^{n-1} \cdot \frac{1}{2}=\left(\frac{1}{2}\right)^{n}$, or if the first $(n-1)$ throws are tails and the $n$-th is heads - the probability of this is also $\left(\frac{1}{2}\right)^{n-1} \cdot \frac{1}{2}=\left(\frac{1}{2}\right)^{n}$; thus

$$
2 \cdot\left(\frac{1}{2}\right)^{n}=\frac{1}{2^{n-1}}
$$

is the probability that we throw $n$ times $(n \geq 2)$. The expected value of the number of necessary throws is, by definition, the weighted average of the number of throws with their probabilities, that is,

$$
M=\sum_{n=2}^{\infty} \frac{n}{2^{n-1}}
$$

We determine the value of $M$ using the sum of the infinite geometric series $\sum_{n=0}^{\infty} \frac{1}{2^{n}}=2$:

$$
\begin{aligned}
M & =\sum_{n=2}^{\infty} \frac{1}{2^{n-1}}+\sum_{n=2}^{\infty} \frac{n-1}{2^{n-1}}=1+\sum_{k=1}^{\infty} \frac{k}{2^{k}}=1+\frac{1}{2}+\sum_{k=2}^{\infty} \frac{k}{2^{k}}= \\
& =\frac{3}{2}+\frac{1}{2} \sum_{k=2}^{\infty} \frac{k}{2^{k-1}}=\frac{3}{2}+\frac{M}{2}
\end{aligned}
$$

thus $M=3$ is the expected value of the number of throws.

Remark. Another way to calculate $M$ (and similar sums) is as follows:

$$
\begin{aligned}
M= & 2 \cdot \frac{1}{2}+3 \cdot \frac{1}{2^{2}}+4 \cdot \frac{1}{2^{3}}+5 \cdot \frac{1}{2^{4}}+\ldots=\left(\frac{1}{2}+\frac{1}{2}\right)+\left(\frac{1}{2^{2}}+\frac{1}{2^{2}}+\frac{1}{2^{2}}\right)+ \\
& +\left(\frac{1}{2^{3}}+\frac{1}{2^{3}}+\frac{1}{2^{3}}+\frac{1}{2^{3}}\right)+\left(\frac{1}{2^{4}}+\frac{1}{2^{4}}+\frac{1}{2^{4}}+\frac{1}{2^{4}}+\frac{1}{2^{4}}\right)+\ldots
\end{aligned}
$$

We write the sums in the parentheses in separate rows, and then sum the elements of the (infinite downward) table not row by row, but column by column; each column sum is an infinite geometric series with a ratio of $\frac{1}{2}$, so

$$
\begin{aligned}
M= & {\left[\frac{1}{2}+\frac{1}{2^{2}}+\frac{1}{2^{3}}+\ldots\right]+\left[\frac{1}{2}+\frac{1}{2^{2}}+\frac{1}{2^{3}}+\ldots\right]+\left[\frac{1}{2^{2}}+\frac{1}{2^{3}}+\frac{1}{2^{4}}+\ldots\right]+} \\
& +\left[\frac{1}{2^{3}}+\frac{1}{2^{4}}+\frac{1}{2^{5}}+\ldots\right]+\ldots=1+1+\frac{1}{2}+\frac{1}{2^{2}}+\ldots=1+2=3
\end{aligned}
$$

II. solution. After the first throw, regardless of its value, we expect the opposite outcome. The probability of this is $\frac{1}{2}$, so on average it happens after two throws. Therefore, on average $1+2=3$ throws are necessary to get both heads and tails.