IMO 1987 - function such that $f(f(n))=n+1987$ 
Show that there is no function $f: \mathbb{N} \to \mathbb{N}$ such that $$f(f(n))=n+1987, \ \forall n \in \mathbb{N}$$

This is problem 4 from IMO 1987 - see, for example, AoPS.
 A: Just a minor variation of the answer by user58512, for fun; like that answer it works even if we replace $\Bbb N$ by $\Bbb Z$. The equation $f(n+1987)=f(n)+1987$ in that answer means that $$f(n+1987)-(n+1987)=f(n)-n,$$ so it shows that $f(n)-n$ only depends on the class of $n$ modulo $1987$. Also the map $\bar f$ that $f$ induces on $\Bbb Z/1987\Bbb Z$ is surjective (the class of $n$ is in the image of $\bar f^2$) hence bijective: $f(n)$ runs over all congruence classes as $n$ runs over all congruence classes modulo $1987$.
Now let $S$ be the sum of $f(n)-n$ over all those congruence classes, obviously an integer. But computing the sum of $1987=f(f(n))-n=\bigl(f(f(n))-f(n)\bigl)+\bigr(f(n)-n\bigr)$ over all those congruence classes, one gets on one hand the odd number $1987^2$, and on the other hand the even number $2S$; contradiction.
A: Hint $\ $mod $\rm\,p\!:\ f(f(n)) = n+p\equiv n\:$ is an involution, hence $\rm\:p\:$ odd $\Rightarrow$ that it has a fixed point $\rm\:f(n)\equiv n,\:$ so $\rm\:f(n) = n\!+\!k p,\ k\in\Bbb Z.\:$ Now the orbit of $\rm\:f\:$ on $\rm\,n\,$ yields a contradiction
$$\rm\begin{array}{rlcl}
    &\rm n &\rm \to &\rm \color{#C00}{n+kp} \\
\to &\rm \color{#0A0}{n\!+\!p} &\rm \to &\rm n\!+\!(k\!+\!1)p \\
\to &\rm n\!+\!2p &\rm \to &\rm n\!+\!(k\!+\!2)p\\
\to &\rm n\!+\!3p &\rm \to &\rm n\!+\!(k\!+\!3)p\\
    &\rm\  \cdots &\rm      &\rm \quad\cdots  \\
\to &\rm \color{#C00}{n\!+\!kp} &\rm \to &\rm n\!+\!(k\!+\!k)p = \color{#0A0}{n\!+\!p}\ \Rightarrow\ 2k=1\ \Rightarrow\Leftarrow\: k\in\Bbb Z\\
\end{array}$$
Remark $\ $ This was a hint  I posted many years ago in another math forum. You can find many further posts here exploiting parity and involutions by searching on involution and Wilson (group theoretical form of Wilson's theorem).
A: I answered a minor variation of this question here:
Functions such that $f(f(n))=n+2015$, unaware it was an IMO question. My proof might be a little shorter than those given here:
Suppose such a function exists. Then define the function
$$g\colon \{1,\dots,1987\}\to \{1,\dots,1987\},\quad g(n)=f(n)\bmod{1987}.$$
Then $g$ is an involution of ${1,\dots,1987}$. Since there are an odd number of elements, it follows that $g$ has a fixed point, say $k$. Then $f(k)=k+1987j$ for some $j$, whereupon $f(f(k))=k+1987\cdot 2j\not=k+1987$, contradiction.
A: Here is an alternative approach. It is obvious that such an $f$ must be an injection. Now, look at sets $\mathbb{N}$, $A = f(\mathbb{N})$ and $B = f(f(\mathbb{N})) = \{n + 1987 \ | \ n \in \mathbb{N}\}$.
It is easy to see that $B \subset A \subset \mathbb{N}$, and also from the injectivity of $f$ that $f$ induces a bijection between the disjoint sets $\mathbb{N} \setminus A$ and $A \setminus B$. Therefore, $\mathbb{N} \setminus B = (\mathbb{N} \setminus A) \cup (A \setminus B)$ must contain an even number of elements. But $|\mathbb{N} \setminus B| = 1987$, which is a contradiction, and we are done.
A: Here is a generalisation.
Proposition. For positive integers $k,l$, the functional equation
$$
  f^k(n)=n+l\qquad\text{for all $n$}
$$
has no solutions $f$ in the functions $\Bbb Z\to\Bbb Z$, or in the functions $\Bbb N\to\Bbb N$, unless $k$ divides $l$.
In the case $k\mid l$, one does of course have solutions, for instance $n\mapsto n+l/k$.
Proof. Assume that $f$ satisfies the functional equation.


*

*For all $n$ one has $f(n+l)=f^{k+1}(n)=f(n)+l$.

*Therefore $f(n+l)-(n+l)=f(n)-n$, and $f(n)-n$ is constant when $n$ varies over any congruence class modulo $l$.

*The image under $f$ of such a congruence class is therefore another congruence class, and $f$ gives rise to a function $\bar f:\Bbb Z/l\Bbb Z\to\Bbb Z/l\Bbb Z$.

*Since $(\bar f)^k(\bar n)=\bar n$ for all $\bar n\in\Bbb Z/l\Bbb Z$, this function $\bar f$ is a bijection.

*Put $S=\sum_{i=0}^{l-1}(f(i)-i)$; note that since $f(i)-i$ is constant on congruence classes modulo $l$, the same sum is obtained if one sums over a different set of representatives of the congruence classes modulo$~l$.

*In particular, since for any $j\in\Bbb N$ the set $\{\,f^j(i)\mid0\leq i<l\,\}$ is a set of representatives of the congruence classes modulo$~l$ (since $(\bar f)^j$ is a bijection), one has $\sum_{i=0}^{l-1}(f^{j+1}(i)-f^j(i))=S$.

*Also $\{\,f(i)\mid0\leq i<l\,\}$ is set of representatives of the congruence classes modulo$~l$, so $\sum_{i=0}^{l-1}f(i)\equiv\sum_{i=0}^{l-1}i\pmod l$, and hence $S\equiv0\pmod l$. So $S=ls$ for some integer$~s$.

*From the functional equation $\sum_{i=0}^{l-1}\bigl(f^k(i)-i\bigr)=\sum_{i=0}^{l-1}l=l^2$.

*One can decompose each term in that sum as a telescopic sum:
$f^k(i)-i=\sum_{j=0}^{k-1}\bigl(f^{j+1}(i)-f^j(i)\bigr)$.

*Therefore
$$l^2=\sum_{i=0}^{l-1}\bigl(f^k(i)-i\bigr)
=\sum_{j=0}^{k-1}\sum_{i=0}^{l-1}(f^{j+1}(i)-f^j(i)\bigr)
=\sum_{j=0}^{k-1}S=kS=kls.$$

*Simplifying by a factor $l$ one gets $l=ks$, proving that $k$ divides $l$.

A: We prove that if $f(f(n)) = n + k$ for all $n$, where $k$ is a fixed positive integer, then $k$ must be even. If $k = 2h$, then we may take $f(n) = n + h$.
Suppose, 
$f(m) = n$ with $m= n (mod  $ k $)$. 
Then by an easy induction on $r$ we find $f(m + kr) = n + kr, f(n + kr) = m + k(r+1)$. 
We show this leads to a contradiction. 
Suppose $m < n$, 
$n = m + ks$ for some $s > 0$ $\implies f(n) = f(m + ks) = n + ks$. 
But $f(n) = m + k, so m = n + k(s - 1) >= n$. Contradiction. 
So we must have $m\ge n$, so $m = n + ks$ for some $s \ge 0$. 
But now,
$f(m + k) = f(n + k(s+1)) = m + k(s + 2)$. 
But $f(m + k) = n + k$, so $n = m + k(s + 1) > n$. Contradiction again.
So if $f(m) = n$, then $m$ and $n$ have different residues $(mod $ k $)$. 
Suppose they have $r_1$ and $r_2$ respectively. 
Then the same induction shows that:
All sufficiently large $s= r_1 (mod k)$ have $f(s)= r_2(mod k)$, 
and that all sufficiently large $s = r_2 (mod k)$ have $f(s) = r_1 (mod k)$. 
Hence if $m$ has a different residue $r(mod k)$, then $f(m) cannot have residue $r_1$ or $r_2$. 
For if f(m) had residue $r_1$, then the same argument would show that all sufficiently
large numbers with residue $r_1$ had $f(m) = r (mod k)$. 
Thus the residues form pairs, so that if a number is congruent to a particular residue, then f of the number is congruent to the pair of the residue. But this is impossible for $k$ odd.
A: 
Proposition. For an integer $n$ and for a positive integer $p$, there exists $f:\mathbb{Z}\to\mathbb{Z}$ such that $$f^p(x)=x+n\tag{*}$$ for all $x\in\mathbb{Z}$ if and only if $n$ is divisible by $p$.  Here, for every function $\phi:A\to A$ on a set $A$, we define $\phi^0:=\text{id}_A$ and $\phi^k:=\phi^{k-1}\circ \phi$  for all $k\in\mathbb{Z}_{\geq 1}$.  

Proof of Proposition. 
Clearly, when $n$ is divisible by $p$, $f$ given by $f(x):=x+\dfrac{n}{p}$ for every $x\in\mathbb{Z}$ fits the bill.  We now assume that that such $f$ exists.  We aim to show that $p\mid n$.
First of all, $f$ is a bijection on $\mathbb{Z}$.  We also have
$$f(x+n)=f\big(f^p(x)\big)=f^{p+1}(x)=f^{p}\big(f(x)\big)=f(x)+n\text{ for any }x\in\mathbb{Z}\,.$$
By induction, $f^k(x+nq)=f^k(x)+nq$ for any $q\in\mathbb{Z}$ and for any $k\in\mathbb{Z}_{\geq 0}$.
Let $R$ denote the ring $\mathbb{Z}/m\mathbb{Z}$ of integers modulo $m$, where $m:=|n|$.  Define $g:R\to R$ via
$$g(x+m\mathbb{Z}):=f(x)+m\mathbb{Z}\text{ for each }x\in\mathbb{Z}\,.$$
Then, $g$ is a well defined permutation on $R$.  Observe that $g^p=\text{id}_R$.  
Write $g$ as a product of disjoint cycles $c_1,c_2,\ldots,c_l$ of lengths $t_1,t_2,\ldots,t_l$ such that $t_1+t_2+\ldots+t_l=m$.  Since $g^p=\text{id}_R$, $c_j^p=\text{id}_R$ for all $j=1,2,\ldots,l$.  This shows that the length $t_j$ of $c_j$ is a divisor of $p$.  
If there exists $r\in\{1,2,\ldots,l\}$ such that $t_r<p$, then $g^{t_r}$ fixes a point, say, $y+m\mathbb{Z}$.  This means $f^{t_r}(y)=y+sn$ for some $s\in\mathbb{Z}$.  However, this shows that
$$\begin{align}f^p(y)&=\left(f^{t_r}\right)^{\frac{p}{t_r}}(y)=\left(f^{t_r}\right)^{\frac{p}{t_r}-1}\big(f^{t_r}(y)\big)=\left(f^{t_r}\right)^{\frac{p}{t_r}-1}(y+sn)\\&=\left(f^{t_r}\right)^{\frac{p}{t_r}-2}\big(f^{t_r}(y+sn)\big)=\left(f^{t_r}\right)^{\frac{p}{t_r}-2}\big(f^{t_r}(y)+sn\big)\\&=\left(f^{t_R}\right)^{\frac{p}{t_r}-2}(y+2sn)=\ldots=y+\frac{p}{t_r}sn\,.\end{align}$$
Nevertheless, $f^p(y)=y+n$ by the given functional equation.  Thus, $\dfrac{p}{t_r}s=1$, which is impossible as $\dfrac{p}{t_r}>1$.  Consequently, $t_j=p$ for every $j=1,2,\ldots,l$.
Hence, $|n|=m=t_1+t_2+\ldots+t_l=pl$.  This shows that $n$ is a multiple of $p$.
$$\phantom{aa} \tag*{$\Box$}$$

Corollary.  Let $n$ be a nonnegative integer and $p$ a positive integer.  There exists $f:\mathbb{Z}_{\geq 0}\to\mathbb{Z}_{\geq 0}$ to (*) for all $x\in\mathbb{Z}_{\geq 0}$ if and only if $p\mid n$.



Corollary.  Let $n$ be a nonnegative integer and $p$ a positive integer.  There exists $f:\mathbb{Z}_{> 0}\to\mathbb{Z}_{> 0}$ to (*) for all $x\in\mathbb{Z}_{> 0}$ if and only if $p\mid n$.



Remark.  If $p\mid n$, all solutions $f:\mathbb{Z}\to\mathbb{Z}$ to (*) can be obtained in the following manner.  Let $m:=|n|$.  Partition $\{0,1,2,\ldots,m-1\}$ into $l:=\dfrac{m}{p}$ sets of size $p$, say, $$\{x_1^1,x_1^2,\ldots,x_1^p\},\{x_2^1,x_2^2,\ldots,x_2^p\},\ldots,\{x_l^1,x_l^2,\ldots,x_l^p\}\,.$$
  For each $j=1,2,\ldots,l$ and $\mu=1,2,\ldots,p$, take $\lambda^\mu_j\in\mathbb{Z}$ arbitrarily such that $$\sum_{\mu=1}^p\,\lambda^\mu_j=1$$ for all $j=1,2,\ldots,l$.  Now, define
  $$f(x_j^\mu):=x_j^{\mu+1}+n\,\lambda^\mu_j$$
  for $\mu=1,2,\ldots,p$ and $j=1,2,\ldots,l$, where $x_j^{p+1}:=x_j^1$.  For other values of $x\in\mathbb{Z}$, we write $$x=\tilde{x}+nq$$ with $\tilde{x}\in\{0,1,2,\ldots,m-1\}$ and $q\in\mathbb{Z}$.  Then, $$f(x)=f(\tilde{x})+nq\,.$$
  We can then see that there are countably infinitely many solutions $f:\mathbb{Z}\to\mathbb{Z}$ to (*) when $p\mid n$.

We can use a similar construction to deal with the situations $f:\mathbb{Z}_{\geq 0}\to\mathbb{Z}_{\geq 0}$ or $f:\mathbb{Z}_{>0}\to\mathbb{Z}_{>0}$.  For given $n\in\mathbb{Z}_{\geq 0}$ and $p\in\mathbb{Z}_{>0}$ such that $p\mid n$, there are in total 
$$\frac{n!}{\left(\frac{n}{p}\right)!}$$ 
functions $f:\mathbb{Z}_{\geq 0}\to\mathbb{Z}_{\geq 0}$ (respectively, $f:\mathbb{Z}_{>0}\to\mathbb{Z}_{>0}$) that satisfy (*) for all $x\in\mathbb{Z}_{\geq 0}$ (respectively, $x\in\mathbb{Z}_{>0}$). 
