Function such that $f(f(f(x))) = f(f(x)) \neq f(x)$ Give an example of a function $f\colon\Bbb R\rightarrow \Bbb R$ such that $f\circ f\circ f=f\circ f\neq f$, that is, $f(f(f(x))) = f(f(x))$ on $\Bbb R$ but $f(f(x))\ne f(x)$ for some $x\in\mathbb R$.
 A: Good old rational indicator function
$$f(x) =\mathbf 1_{\mathbb Q}(x)= \begin{cases} 1 & \text{if $x\in\mathbb{Q}$} \\ 0 & \text{otherwise} \\ \end{cases} $$
does the trick.
A: You may try the continuous function $f(x)=|x|-x$. Then $f(f(x))=0$.
A: Given any integer $n\ge2$, we can define $f:\mathbb R\to\mathbb R$ such that $f,f^2,\ldots,f^n$ are distinct functions but $f^n=f^{n+1}$. Your question is just the case where $n=2$.
We first define $f$ on a smaller domain and then extend its definition to the whole real line. Let $f:\{0,1,\ldots,n+1\}\to\mathbb R$ be defined by
$$f(x)=\begin{cases}
x,&x=0,1,\\
x-1,&x=2,3,\ldots,n+1.
\end{cases}$$
Then $f^k(n+1)=n+1-k$ for $k=1,2,\ldots,n$. Hence $f,f^2,\ldots,f^n$ are distinct functions. However, as $f^n(0)=0$ and $f^n(x)=1$ for all $x>0$, we have $f^{n+1}=f^n$.
Next, we extend the definition of $f$ to the nonnegative real line. When $x\ge0$ but $x\not\in\{0,1,\ldots,n+1\}$, we define $f$ such that
\begin{cases}
f(x)=x,&x\in(0,1),\\
f(x)\le k-1,&x\in(k-1,k),\,k=2,3,\ldots,n+1,\\
f(x)\le n,&x>n+1.
\end{cases}
(Note that a $f$ can be defined as a continuous function, although this is not required by the OP.) Then $f(\mathbb R_{\ge0})\subset[0,n],\,f^2(\mathbb R_{\ge0})\subset[0,n-1],\,\ldots$ and so on, so that $f^n(\mathbb R_{\ge0})\subset[0,1]$. Since $f$ is the identity function on $[0,1]$, we have $f^{n+1}=f^n$.
Finally, when $x<0$, we define $f(x)=x$ or $f(x)=-f(-x)$.
