I have 100 cards labelled from 1 to 100. I place this in a row with all the cards face down. On the first turn I flip the first card. Now this card has a number $n$ on it. I then put this card face-up in the $n$-th position, and flip the original $n$-th card. I then continue this process until I eventually get back the card labelled 1. I will call this a cycle. Now I repeat this process by flipping the next card (from the start) that is not yet face up and continue this process until all 100 cards are face up.
After this process is completed, what is the average number of cycles that I obtain? (An example of some cycles could include $1\rightarrow41\rightarrow57\rightarrow79\rightarrow1$, $2\rightarrow65\rightarrow35\rightarrow71\rightarrow2$, $3\rightarrow3$, etc.
Instead of considering $100$ cards, we assume there are $n$ cards, where $n \geq 1$. Let $X_n$ denotes the # of cycles given $n$ cards and we are interested in the following identity $\mathbb{E}[X_n]$. We also introduce another random variable, denoted as $L$, which denotes the length of the first cycle, thus $L \in [1, n]$. We use the following fact of conditional expectation: $$ \mathbb{E}[X_n] = \sum_{i=1}^n \mathbb{E}[X_n\mid L = i]\cdot\Pr[L = i] \tag{1} $$
We first show how to compute $\Pr[L = i]$. This is in fact not a difficult task.
We next show how to compute $\mathbb{E}[X_n \mid L = i]$. If the first cycle is of length $i$, the expectation of $X_n$ is just the expectation of # of cycles in the remaining $n - i$ cards plus $1$, i.e., $$ \mathbb{E}[X_n \mid L = i] = \mathbb{E}[X_{n-i}] + 1 \tag{3} $$
To sum up, we have $$ \mathbb{E}[X_n] = \sum_{i=1}^n (\mathbb{E}[X_{n-i}] + 1)\cdot\frac{1}{n} = 1 + \frac{1}{n}\sum_{i=1}^n\mathbb{E}[X_{n-i}]\tag{4} $$ Specially, we have $$ \mathbb{E}[X_0] = 0 \text{ and } \mathbb{E}[X_1] = 1 $$ From $(4)$, we can obtain $$ n\mathbb{E}[X_n] = n + \sum_{i=1}^n\mathbb{E}[X_{n-i}] \tag{5} $$ and $$ (n-1)\mathbb{E}[X_{n-1}] = n - 1 + \sum_{i=1}^{n-1}\mathbb{E}[X_{n-1-i}] \tag{6} $$ Substracting $(6)$ from $(5)$ results in: $$ n \mathbb{E}[X_n] - (n-1)\mathbb{E}[X_{n-1}] = 1 + \mathbb{E}[X_{n-1}] \Rightarrow \mathbb{E}[X_n] = \mathbb{E}[X_{n-1}] + \frac{1}{n} $$ Consequenlty, we conclude that $$ \mathbb{E}[X_n] = \sum_{i=1}^n \frac{1}{i} = H_n $$