Nowhere continuous functions $f: \mathbb{R} \to \mathbb{R}$ such that $f(f(x)) = (f(x))^2$

Are there any nowhere continuous functions $f: \mathbb{R} \to \mathbb{R}$ satisfying the identity $f(f(x)) = (f(x))^2$ $\forall x \in \mathbb{R}$?


Solution 1:

Take some indicator-like function $$ f\colon \Bbb R \to \Bbb R, x\mapsto \begin{cases}1 &\text{if}\ x\in\Bbb Q,\\ -1 &\text{if}\ x\in\Bbb R\setminus \Bbb Q.\end{cases} $$ Then $f(-1) = f(1) = 1 = (-1)^2=1^2$.

Solution 2:

Perhaps useful to see where this example comes from. The first step is to note that if you have a function of the form $$ g_{a, b}(x) = a\mathbf{1}_{\mathbb{Q}}(x) + b\mathbf{1}_{\mathbb{R} \setminus \mathbb{Q}}(x), $$ then this is everywhere discontinuous provided that $a \neq b$. Additionally, the condition $f(f(x)) = f(x)^2$ is equivalent to $$ a^2 = g_{a,b}(a) \quad \mbox{and} \quad b^2 = g_{a,b}(b). $$ We can simplify by assuming $a, b$ are rational. (Note if you are curious, you can check that if $a$ or $b$ is irrational, then the functional equation $f \circ f = f^2$ cannot be satisfied.) Then we need $$ a^2 = a \quad \mbox{and} \quad b^2 = a. $$ Hence any pair $(a, b) \in \mathbb{Q} \times \mathbb{Q}$ will work provided $a \neq b$, and $a^2 = b^2 = a$; the only solution to this is if $a = 1, b= -1$. This yields $$ f = g_{1, -1} = \mathbf{1}_{\mathbb{Q}} - \mathbf{1}_{\mathbb{R} \setminus \mathbb{Q}}. $$