Find a real function $f:\mathbb{R}\to\mathbb{R}$ such that $f(f(x)) = -x$? I've been perusing the internet looking for interesting problems to solve. I found the following problem and have been going at it for the past 30 minutes with no success:


  Find a function $f: \mathbb{R} \to \mathbb{R}$ satisfying $f(f(x)) = -x$ for all $x \in \mathbb{R}$.


I am also wondering, can we find $f$ so that is continuous?

I was thinking of letting $f$ be a periodic function, and adding half the period to x each time. I had no success with this, and am now thinking that such a function does not exist.

Source: http://www.halfaya.org/Casti/CalculusTheory2/challenge.pdf
An important piece of information is:

Theorem: $f$ is not continuous.

Proof: Observe that $f$ is invertible, because

$$f(f(f(f(x)))) = f(f(-x)) = x$$

and so $f \circ f \circ f = f^{-1}$. Any continuous invertible function on $\mathbb{R}$ is either strictly increasing or strictly decreasing.

If $f$ is strictly increasing, then:


$1 &lt; 2$
$f(1) &lt; f(2)$
$f(f(1)) &lt; f(f(2))$
$-1 &lt; -2$


contradiction! Similarly, if $f$ is strictly decreasing, then:


$1 &lt; 2$
$f(1) &gt; f(2)$
$f(f(1)) &lt; f(f(2))$
$-1 &lt; -2$


contradiction! Therefore, we conclude $f$ is not continuous. $\square$



For the sake of completeness, the entire solution space for $f$ consists of functions defined as follows:


Partition the set of all positive real numbers into ordered pairs $(a,b)$
Define $f$ by, whenever $(a,b)$ is one of our chosen pairs,

$f(0) = 0$
$f(a) = b$
$f(b) = -a$
$f(-a) = -b$
$f(-b) = a$



To see that every solution is of this form, let $f$ be a solution. Then we must have $f(0) = 0$ because:


Let $f(0) = a$. Then $f(a) = f(f(0)) = 0$ but $-a = f(f(a)) = f(0) = a$, and so $f(0) = 0$


If $a \neq 0$, then let $f(a) = b$. We have:


$f(b) = f(f(a)) = -a$
$f(-a) = f(f(b)) = -b$
$f(-b) = f(f(-a)) = a$


From here it's easy to see the set $\{ (a,f(a)) \mid a&gt;0, f(a)&gt;0 \}$ partitions the positive real numbers and so is of the form I describe above.



One particular solution is

$$ f(x) = \begin{cases}
   0   &amp; x = 0
\\ x+1 &amp; x &gt; 0 \wedge \lceil x \rceil \text{ is odd}
\\ 1-x &amp; x &gt; 0 \wedge \lceil x \rceil \text{ is even}
\\ x-1 &amp; x &lt; 0 \wedge \lfloor x \rfloor \text{ is odd}
\\ -1-x &amp; x &lt; 0 \wedge \lfloor x \rfloor \text{ is even}
\end{cases}$$

e.g. $f(1/2) = 3/2$, $f(3/2) = -1/2$, $f(-1/2) = -3/2$, and $f(-3/2) = 1/2$.

(This works out to be Jyrki Lahtonen's example)
Closed form and generalised solutions
The piecewise function referred to in user14972's, Jyrki's and Alex's answers can be given in the closed form $$f(x)=\operatorname{sign}\left(x\right)-\left(-1\right)^{\operatorname{ceil}\left|x\right|}x$$
where $\operatorname{ceil}$ is the ceiling function. Let $n\in\mathbb{N}_0,\ \alpha\in(0,1]$, such that all $x\in\mathbb{R}^+$ can be represented by either $(2n+\alpha)$ or $(2n+1+\alpha)$. By substitution, we have $f(2n+\alpha)=2n+1+\alpha$ and $f(2n+1+\alpha)=-2n-\alpha$. Since $f$ is odd, we also have $f(-(2n+\alpha))=-(2n+1+\alpha)$ and $f(-(2n+1+\alpha))=2n+\alpha$, and therefore have the following cycle of values under $f$. Along with the trivial cycle $f(0)=0$, this proves that $f(f(x))=-x$ for all $x\in\mathbb{R}$.
$$\require{AMScd}
\begin{CD}
    2n+\alpha @&gt;&gt;&gt; 2n+1+\alpha\\
    @AAA @VVV\\
    -2n-1-\alpha @&lt;&lt;&lt; -2n-\alpha
\end{CD}$$
We may generalise this solution to $f^*(x)=c\cdot f\left(\frac{x}c\right)$, for all $c\in\mathbb{R}/\{0\}$. Even more general permissible functions may be defined by defining each cycle separately, as in the figure below, where $a,b$ are arbitrary real values.
$$\require{AMScd}
\begin{CD}
    a @&gt;g&gt;&gt; b\\
    @AAA @VVhV\\
    -b @&lt;&lt;&lt; -a
\end{CD}$$
The function, $g$, that transitions through the cycle can be defined quite broadly, and may not necessarily be even locally continuous. However it must not map $x$ to itself, nor $-x$, and cannot overlap with another cycle. The cycle is completed by defining $h$ as the negative inverse of $g$ and then defining both $g$ and $h$ as odd functions. For instance, if we define $g(x)=ax$ with $a&gt;1$, we would have $h(x)=-\frac{x}{a}$ and $$f(x)=\frac{x}{2a}\left(\left(a^{2}+1\right)\left(-1\right)^{\operatorname{ceil}\left(\log_{a}\left|x\right|\right)}+a^{2}-1\right)$$
for nonzero $x$, which is admits another solution if we take $f(0)=0$. We can methodically generate solutions with this method by picking $g(x)$ as a continuous, strictly increasing function from $\mathbb{R}^+$ to itself, that is greater than $x$. If we then take a seed value $c\in\mathbb{R}^+$, we may partition $\mathbb{R}^+$ into the two disjoint sets,
$$
S_1=\bigcup_{n\in\mathbb{Z}}\ (g^{2n},g^{2n+1}],\quad 
S_2=\bigcup_{n\in\mathbb{Z}}\ (g^{2n-1},g^{2n}]
$$
where $g^n$ denotes the $n$th iteration of $g$ on $c$ and where negative superscripts denote iterated inverses, e.g. $g^{-2}=g^{-1}\circ g^{-1}(c)$. We then have that whenever $x\in S_1$, $g(x)\in S_2$ and vice versa that $x\in S_2$ implies $g(x)\in S_1$. We can then define the solution $$f(x)=\begin{cases}0,&amp;x=0\\g(x),&amp;x\in S_1\\-g^{-1}(x),&amp;x\in S_2\\-f(-x),&amp;\text{else}\end{cases}$$