Smooth or analytic functions which keep $\mathbb{Z}$ invariant Is there a smooth or real analytic   function $f: \mathbb{R}\to \mathbb{R}$ such that $f(\mathbb{Z})\subseteq \mathbb{Z}$  but $f$ is not a polynomial with  integer  coefficients? 
If the answer is yes we generalize the question as follows:
Is there a smooth or real analytic   function $f: \mathbb{R}\to \mathbb{R}$ such that $\forall n \in \mathbb{N} \cup \{0\},\;\;\;f^{(n)}(\mathbb{Z})\subseteq \mathbb{Z}$  but $f$ is not a polynomial with  integer  coefficients.
In the above by $f^{(n)}$  we mean the $n$th derivative of $f$.
 A: There exists a nontrivial smooth (but not analytic) function that is identically 0 outside of $[1/3,2/3]$. Call this function $\phi$. Then one example is
$$ \Phi(x) = \sum_{k\in\mathbb Z}\phi(x-k)$$
with $\Phi^{(n)}(\mathbb Z) = \{ 0 \} \subset \mathbb Z$ for any $n$. Note that the functions that leave $\mathbb Z$ invariant form a vector space, so that if we define  $\tilde\Phi$ by
$$ \tilde\Phi(x) = \Phi(x) + x$$
We have also achieved $\tilde\Phi(\mathbb Z) = \mathbb Z$ .
For a non-polynomial analytic answer, consider the function $F(x) := \Gamma(1+x^2)$  with $F(\mathbb Z)\subset \text{Factorial}(\mathbb N^2) \subset \mathbb Z$ and is analytic everywhere.
A: Some insights on building smooth functions thanks to convolution and mollifier.
Let consider the following mollifier $$\phi_\epsilon(x):\begin{cases}\exp\left(1-\dfrac 1{1-(\frac x\epsilon)^2}\right)& x\in]-\epsilon,\epsilon[\\0&\text{otherwise}\end{cases}$$


*

*$\phi_\epsilon$ is $C^\infty$ with compact support $[-\epsilon,\epsilon]$

*$\phi_\epsilon^{(2n+1)}(0)=0$ 

*$\displaystyle\phi_\epsilon^{(2n)}(0)=\frac{(2n)!}{n!\,\epsilon^{2n}}$ 


In particular for $\epsilon=\frac 1p$ then $\phi_\epsilon^{(n)}(0)\in\mathbb Z$ for any $n$, also $\phi(0)=1$.
Let's define the following distribution $$U(x)=\sum\limits_{i=-\infty}^{\infty} u_i\delta_i(x)$$ where $u_i\in\mathbb Z$ and $\delta_i$ the translated dirac $\delta_i(x)=\tau_i\delta(x)=\delta(x-i)$
Now let's consider $$f=U*\phi_\epsilon\qquad f(x)=\int_\mathbb Rf(x-t)\phi_\epsilon(t)\mathop{dt}$$

By the properties of convolution we get that: $\quad$$f$ is a smooth function.
Since for any $g$ we have $g*\delta_i=g*(\tau_i.\delta)=(\tau_i.g)*\delta=\tau_i.g$
Then $$f(x)=\sum\limits_{i=-\infty}^{\infty}u_i\phi_\epsilon(x-i)$$
In particular 


*

*$f(i)=u_i\quad$ where the $u_i$ are any desired integer.

*$f^{(n)}(i)\in\mathbb Z$ according to the properties shown before for $\phi_\epsilon$ with $\epsilon=\frac 1p$.



Here I have chosen a very simple distribution $U$ to start with, but you are free to experiment with other stuff, the main idea to remember is that $U*\phi_\epsilon$ will always be a smooth function, because the convolution give $f^{(n)}=D^n(U*\phi_\epsilon)=U*{\phi_\epsilon}^{(n)}$ so since $\phi$ is $C^\infty$ then $f$ heritates this smoothness for all derivatives.
Although with a more complicated $U$ it may be a little difficult to decide whether the $f^{(n)}(i)$ are integer or not.
A: The first part is easy: $f(x)=\sin(\frac{\pi}{2}x)$.
For the second part, see the Smooth transition functions section of this wiki article: https://en.wikipedia.org/wiki/Non-analytic_smooth_function
