I Have to find the number of functions $f(x)$ from {1,2,3,4,5} to {1,2,3,4,5} that satisfy $ f(f(x)) = f(f(f(x)))$ for all $x$ in {1,2,3,4,5}.
However I am unable to understand from where to start. The number of cases I am trying to analyze is too much. I tried classifying it into functions which are onto, which are not unto wherein only 1 image appears twice and so on but I am realising that the list is becoming too huge. Can anyone help me with a simpler analysis of this question.
Hint: If $x=f^2(c)$ for some $c$, then you have $f(x) = x$. This means that every $x \in \text{Im}(f^2)$ is a fixed point of $f$. Do you think you can conclude?
We can build $f$ as follows.
$\quad\mathbf{(1)}$: $f$ must contain exactly $k$ fixed points in $\{1,2,\dots,5\}$ for some $k$ with $1\leqslant k\leqslant 5$.
$\quad\quad\mathbf{(1.1)}$: If $k=5$, then there is only one possibility: $f$ is the identity.
$\quad \mathbf{(2)}$: For $k<5$, consider the set $S$ of points that are not fixed and that are mapped to one of the $k$ fixed points. It is not empty, for otherwise we could iterate some non-fixed $x$ under $f$ and find a contradiction with $f^2 = f^3$. Let $j=|S|$, so that $1\leqslant j\leqslant 5-k$.
$\quad \mathbf{(3)}$: The remaining points, if any, must map to one of the $j$ points in $S$. Indeed, let $x$ be one such point, and consider $f(x)$. We have that $f(f(x)) = f^2(x)$ is a fixed point of $f$ $($because $f^2 = f^3)$, so either $f(x)\in S$ or $f(x)$ is itself a fixed point. The latter would in turn imply either $x\in S$ or else $x$ is itself a fixed point, but we've excluded these possiblities (the 'remaining' points).
We hence have
$$1 + \sum_{k=1}^4\left(\binom{5}{k}\cdot\sum_{j=1}^{5-k}\,\binom{5-k}{j}k^jj^{5-k-j}\right) = 756$$
possibilities. Let's explain the formula.
Notice that cancellations lead to the formula in Somos's answer, so here's a combinatorial interpretation for it.
It is easy to generalize this procedure for other values of $m$ with $f^m = f^{m+1}$, and this is basically a tiered approach. For some $x\in\text{Dom}(f)$, let $\text{ord}(x)$ be the smallest non-negative integer $n$ with $f^n(x) = f^{n+1}(x)$, where $f^0(x) = x$. Then it's easy to check that:
In this way, we can build $f$ from the ground up. Mimicking the bullet points above, we proceed as follows: