Does there exist a continuous function such that f(x) is rational for every irrational x, and f(x) is irrational for every rational x?

Does there exist a continuous function such that f(x) is rational for every irrational x, and f(x) is irrational for every rational x? I can think of examples where this would be true, but I'm having a lot of trouble with the continuity.

• @HamidRezaEbrahimi The question is simple and presented answers are OK. Why do you think the question is really deep and needs much more attention? – Alex Ravsky Sep 22 '17 at 23:18
• – Alex Ravsky Sep 23 '17 at 1:04

There is no such a function. Your hypothesis imply that the image of $f$ is countable which is impossible for any non-constant continuous function from $\mathbb R$ into $\mathbb R$.

That's elegant. The image of the irrationals is countable by hypothesis, and the image of the rationals is countable because the rationals are countable. The continuous image of the reals is either constant or an interval (which may be unbounded), and intervals are uncountable.

Suppose such a function $f(x)$ existed. Define $g(x)=xf(x)$. Observe that $g$ is continuous if $f$ is continuous, and whenever $x\not=0$ and $f(x)\not=0$, then $g(x)$ is a product of a nonzero irrational and a nonzero rational, hence it is irrational.

Let $z$ be a nonzero rational rational number. Then $f(z)$ is irrational, so $g(z)$ is nonzero. Hence, $g(z)$ is nonzero in a neighborhood of $z$.

Let $A$ be the set of points where $g(z)$ is nonzero (this includes all of $\mathbb{Q}$ except for $0$). Since $g$ is continuous, this is an open set. Let $I$ be the maximal subset of $A$ containing $1$. Then $I$ is of one of two forms, $I=(a,b)$ or $I=(a,\infty)$, we note that $a>0$ since $0$ is excluded from $A$.

We begin by proving that $g$ is constant on $I$. Suppose that $g$ were not constant on $I$, then there are $b$ and $c$ so that $g(b)\not=g(c)$. Since, between any two numbers, there is a rational number, let $w$ be a rational number between $g(b)$ and $g(c)$. By the intermediate value theorem, there is some $d$ between $b$ and $c$, so that $g(d)=w$. This, however, is impossible because $g(x)$ is either $0$ or irrational.

Now, since $I$ is maximal, we know that $g(a)=0$ (otherwise, $I$ could be extended). But then, by continuity, $0=g(a)=\lim_{x\rightarrow a^+}g(x)=g(1)$ since $g$ is constant on $I$. But, this is a contradicton since $g(1)$ is nonzero.

I think the four year old answers did it.

If $w$ is irrational then $f(w)$ must be rational so there are only countably many such values. If $v$ is rational there are only countably many rationals so there are only countably many such $f(v)$. So there are only countably many possible outputs for $f$.

But $f(0) = a\not \in \mathbb Q$ and $f(\pi) = b \in \mathbb Q$ and $a \ne b$. So by intermediate value theorem there are values where $f(x_c) = c$ for all possible values of $c$ between $a$ a $b$. But there are uncountably many values between $a$ and $b$ so that is impossible

As both Azarel and Syd Henderson pointed out 4 years ago.