TITLE: $f(x)=ax+b$ for some $a,b\in\mathbb{Q}$ if $f(\mathbb{Q})\subset\mathbb{Q}$ and $f(\mathbb{R-Q})\subseteq \mathbb{R}-\mathbb{Q}$
QUESTION [16 upvotes]: let $f$ be a polynomial function such that $f(\mathbb{Q})\subset \mathbb{Q}$ and $f(\mathbb{R}-\mathbb{Q}) \subset \mathbb{R}- \mathbb{Q}$ . Prove that $f(x)=ax+b$ for some $a,b\in\mathbb{Q}$ 
My approach :
I started by defining $g(x)=f(x)-f(0)$ . 
Now $g(x)$ satisfies the properties $g(\mathbb{Q})\subset \mathbb{Q}$ ,  $g(\mathbb{R}-\mathbb{Q})\subset\mathbb{R}-\mathbb{Q}$ and $g(0)=0$ .
$g$ is a polynomial and $g(0)=0$ . $0$ is root of $g$ and hence $g(x)=x h(x)$ for some polynomial $h(x)$ . It will be enough if one can prove that $h(x)$ is constant . I'm stuck here . 
Any form help will be highly appreciated.

REPLY [10 votes]: As in Eric Wofsey's answer, we prove that $f$ has rational coefficients. Multiplying by a constant, we may then assume that $f$ has integer coefficients. Let its leading term be $ax^p$. Replacing $f(x)$ with $a^{p-1}f(x/a)$, we may assume that $f(x)$ is monic with integer coefficients.
If $f$ has degree $> 1$, then we have $f'(x) > 1$ for large enough $x$, say for $x \geq N$, where $N$ is an integer. Then $f(N)$ and $f(N+1)$ are integers and $f(N+1) - f(N) > 1$ by the mean value theorem. By the intermediate value theorem, we must have $f(x) = f(N) + 1$ for some $x \in (N,N+1)$. By hypothesis, $x$ must be rational, hence an integer, since $f$ is monic. But this contradicts the choice of $x$.