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
 A: Here is a graph of one such function, piecewise continuous.

Here's another picture to clarify endpoint behavior.

A: Hint: If all the real numbers in existence were $1,2,-1,-2$, then the function $f: 1\mapsto 2\mapsto -1\mapsto-2\mapsto 1$ would work.
Spoiler:

 Similarly permute $t,1+t,-t,-1-t$ for all $t\in(2n,2n+1]$ and for all natural numbers $n$. Handle $x=0$ separately.

A: Another way of phrasing @Haskell Curry's proof:
Assuming the axiom of choice, both $\Bbb{R}$ and $\Bbb{C}$ are $\Bbb{Q}$-vector spaces with bases of the same cardinality. Thus there is some $\Bbb{Q}$-linear isomorphism $\alpha:\Bbb{R} \to \Bbb{C}$.
Then we can let $f$ be $\alpha^{-1} \circ g \circ \alpha$, where $g(z)=iz$.
A: 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 @>>> 2n+1+\alpha\\
    @AAA @VVV\\
    -2n-1-\alpha @<<< -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 @>g>> b\\
    @AAA @VVhV\\
    -b @<<< -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>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,&x=0\\g(x),&x\in S_1\\-g^{-1}(x),&x\in S_2\\-f(-x),&\text{else}\end{cases}$$
A: 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 < 2$

*$f(1) < f(2)$

*$f(f(1)) < f(f(2))$

*$-1 < -2$


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


*

*$1 < 2$

*$f(1) > f(2)$

*$f(f(1)) < f(f(2))$

*$-1 < -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>0, f(a)>0 \}$ partitions the positive real numbers and so is of the form I describe above.

One particular solution is
$$ f(x) = \begin{cases}
   0   & x = 0
\\ x+1 & x > 0 \wedge \lceil x \rceil \text{ is odd}
\\ 1-x & x > 0 \wedge \lceil x \rceil \text{ is even}
\\ x-1 & x < 0 \wedge \lfloor x \rfloor \text{ is odd}
\\ -1-x & x < 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)
A: We can even find an $ f $ that is $ \mathbb{Q} $-linear, although we need the Axiom of Choice (AC).


*

*As $ \mathbb{R} $ is a $ \mathbb{Q} $-vector space, use AC to obtain a basis $ \beta $ of $ \mathbb{R} $ over $ \mathbb{Q} $.

*Next, form a partition $ \mathcal{P} $ of $ \beta $ into uncountably many two-element subsets.

*Define a $ \mathbb{Q} $-linear function $ f: \mathbb{R} \to \mathbb{R} $ by defining it on $ \beta $ as follows and then extending the definition by linearity:
$$
\forall \{ a,b \} \in \mathcal{P}: \quad f(a) = -b \quad \text{and} \quad f(b) = a.
$$

*We thus obtain
\begin{align}
\forall \{ a,b \} \in \mathcal{P}: \quad f(f(a)) &= f(-b) = - f(b) = -a \quad \text{and} \\
                                         f(f(b)) &= f(a) = -b.
\end{align}

*Therefore, $ f(f(x)) = -x $ for all $ x \in \beta $. By linearity, $ f(f(x)) = -x $ for all $ x \in \mathbb{R} $.

Note: This argument shows that there exist uncountably many $ \mathbb{Q} $-linear $ f: \mathbb{R} \to \mathbb{R} $ satisfying $ f \circ f = - \text{id}_{\mathbb{R}} $ (due to the uncountably many ways of forming $ \mathcal{P} $). As any $ f $ satisfying $ f \circ f = - \text{id}_{\mathbb{R}} $ cannot be continuous, it follows that any such $ f $ that is $ \mathbb{Q} $-linear cannot be Lebesgue-measurable (see these notes).
A: In the comments to @Hurkyl's excellent answer (which really hits the original question out of the park) the question was raised whether the solution must have infinitely many points of discontinuity. Hurkyl's proof only shows that $f$ is discontinuous, and although the solution he presents has infinitely many discontinuities (nothing wrong with that), I don't see "an easy corollary" that any answer must have an infinite number of discontinuities.
Here's a sketch of a proof that an infinite number of discontinuities are indeed needed. If I'm doing this the hard way and there is indeed a two-thought proof, I'm really curious to hear it!
$f$ must have an infinite number of discontinuities
The idea: we must arrange open line intervals on 4-cycles and boundary points on 4-cycles.  For the former there must be $0 \pmod 4$ intervals, but for the latter there must $2 \pmod 4$ intervals; so we can't finitely have both at the same time. 
Proof sketch: 


*

*From @Hurkyl's proof: $f$ is a bijection, $f(0) = 0$, and every nonzero point is on a 4-cycle, i.e., $f^n(x) = x$ iff $n = {0\pmod 4}$.

*Let $D$ be the set of non-zero discontinuities of $f$. If $D$ is finite, $D\cup \{0\}$ separates $\mathbb{R}$ into a set $X$ of $|D| + 2$ open intervals (proof by induction). 

*$f$ is piecewise continuous on these intervals, and must map $X$ onto $X$ and $D$ onto $D$. Proof: take a partition of $\mathbb{R}$ into the maximal continuous subsets (points or intervals) that make $f$ continuous; consider it as a topological space under the induced topology (i.e., the usual topology on each component); $f$ is a topological isomorphism (continuous bijection) and must map boundary points to boundary points.

*Every interval in $X$ must be on a 4-cycle. (proof: every interval $L \in X$ has non-varying sign. If $f(L) = L$ or $f^2(L) = L$, then
for $x \in L, f^2(x)$ has the same sign as $x$. Hence $L$ cannot be on a 1- or 2-cycle.) 
Therefore there must be $0 \pmod 4$ intervals.

*$f$ is a bijection on the points of $D$, which must also form 4-cycles. It follows that $|D| = 0 \pmod 4$. By step 2, we have $2 \mod 4$ intervals.
In short, there's no way to partition the real line so that both the intervals and the boundary points form 4-cycles, unless $D$ is infinite. We conclude that all solutions must have infinitely many points of discontinuity. (The constructive proof given by @Hurkyl shows that solutions do exist.)
