TITLE: Polynomial $P(x)$ with $P(x)\in\mathbb Q$ iff $x\in \mathbb Q$
QUESTION [8 upvotes]: Find all the polynomials $P \in \mathbb{R}[X]$ such that: $P(x) \in \mathbb{Q}$ if and only if $x \in \mathbb{Q}$.

I think $P(x)=x+c$ or $P(x)=-x+c$ where $c$ is some rational constant. But I have no idea approach this.

REPLY [8 votes]: (Lemma 1) If a polynomial $f\in \mathbb{R}[x]$ satisfies $a\in \mathbb{Q} \implies f(a)\in \mathbb{Q}$. Then $f(x)\in\mathbb{Q}[x]$.
(Lemma 2) There does not exist $f(x)\in \mathbb{Q}[x]$ with $\deg f \geq 2$, such that for every rational number $r$, all roots of $f(x)-r = 0$ are rational.


Using these lemmas, we can prove your conjecture (that is $P(x) = a +b x$ for rational $a,b$). Let $P(x)\in \mathbb{R}[x]$ such that $x\in \mathbb{Q} \iff P(x)\in  \mathbb{Q} $, then Lemma 1 says $P(x)\in \mathbb{Q}[x]$. For any rational number, consider the equation $P(x) = r$, it cannot have any irrational solution. Hence Lemma 2 says $\deg P < 2$.  

(Proof of Lemma 1)
Assume $f(x) = a_n x^n + a_{n-1} x^{n-1} + \cdots + a_0$, then 
$$\begin{pmatrix}1 & 1 & \cdots & 1 \\ 2^n & 2^{n-1} & \cdots & 1 \\ \vdots & \vdots & \ddots & \vdots \\
(n+1)^n & (n+1)^{n-1} & \cdots & 1 \end{pmatrix}\begin{pmatrix}a_n \\ a_{n-1} \\ \vdots \\ a_0 \end{pmatrix} = \begin{pmatrix}f(1) \\ f(2) \\ \vdots \\ f(n+1) \end{pmatrix} \in \mathbb{Q}^n $$
Since the matrix is invertible (Vandermonde determinant), the proof is completed.
(Proof of Lemma 2)
I shall call a sum of a rational number $r$ of the form $$r = r_1+\cdots+ r_k$$ with $r_i \in \mathbb{Q}$ and absolute values of their denominators $\leq|N|$ a decomposition of $r$ with size $N$. Note that any rational number can admit only finitely many decompositions with a fixed size. 
Back to the lemma, via multiplying by an integer, we can assume $f(x) = A x^n + B x^{n-1} + \cdots + C$ has integer coefficients. Then for any integer $m$, $f(x)=m$ has all roots rational. We choose distinct $m_1, m_2, ... \in \mathbb{Z}$ such that $C-m_i$ is prime. Then the roots of $f(x)=m_i$ are of the form $\frac{1}{v}$ or $\frac{C-m_i}{v}$, with $v\mid A$. Consider the sum of these roots, for each $m_i$, we obtain a decomposition of $\frac{-B}{A}$ with size $A$. Since $m_i$ are pairwise distinct, these decompositions must also be pairwise distinct. But this contradicts to the finiteness noted above. So such polynomial cannot exist.