Prove that if $f: \mathbb{R} \to \mathbb{Q}$ is continuous then $f$ is constant

Suppose that $f: \mathbb{R} \to \mathbb{R}$ is continuous and that $f(x) \in \mathbb{Q}$ for all $x \in \mathbb{R}$. Prove that f is constant.

I have the idea that because there is an irrational number between any two rational number then if the function is continuous, the function must be constant. But I don't know how to write out a proper proof for it.

Any help is appreciated. Thanks in advance.


Solution 1:

You can use the intermediate value theorem. Suppose $f(x_1) = q_1$ and $f(x_2) = q_2$. Because $f$ is continuous, by the intermediate value theorem, $f$ takes on all values between $q_1$ and $q_2$. If $q_1 \ne q_2$ then one of these values is irrational, which is impossible, so $q_1 = q_2$. Therefore for all $x_2$, $f(x_2) = f(x_1)$ - in other words, $f$ is constant.

Solution 2:

Since $f$ is continuous and $\mathbb{R}$ is connected, then $f(\mathbb{R})$ is connected. Since the only non-empty connected subsets of $\mathbb{Q}$ are single points, we must have that $f$ is constant.

Solution 3:

Contrary;Supposed $f$ is not constant, so without losing of general we suppose that $f$ has two value $x_{0},x_{1}\in Q$ such taht $x_{0}\neq‎x_{1}$, thus $\{x_{0}\}$ and $\{x_{1}\}$ make disconected for $f(R)$.This is contradiction,since $f(R)$ is conected