# Does there exist a continuous function $f: \Bbb R\to \Bbb R$ that is rational at (Lebesgue) almost every irrational, and irrational at every rational?

Does there exist a continuous function $$f: \mathbb{R} \rightarrow \mathbb{R}$$ that is rational at (Lebesgue) almost every irrational, and irrational at every rational?

Some thoughts: for some $$q\in \mathbb{Q}$$, $$f^{-1}(q)$$ is a closed set of positive measure disjoint from the rationals. But of course that's possible. Though it does at least mean that the null set of irrationals where $$f$$ is irrational is dense in $$\mathbb{R}$$, by the Baire Category theorem, since the set is the complement of the countable union of nowhere dense sets: $$\big(\mathbb{Q}\cup(\cup_{q\in\mathbb{Q}} f^{-1}(q))\big)^c$$

Also if it exists, $$f$$ can't be injective on any open interval. So the Cantor function comes to mind, but that obviously doesn't work since it's locally constant on an open set. Maybe a "nowhere injective" function is guaranteed to be locally constant somewhere? I have no idea.

(I saw this asked on reddit, and after thinking for a while I'm too curious not to ask here.)

• How is it possible that there is a closed (sub)set (of the reals) of positive (Lebesgue) measure disjoint from the rationals? – Cameron Buie May 10 at 1:55
• @CameronBuie for instance for any $\epsilon>0$, you can enumerate the rationals $q_n$ and take the complement of $\cup_nB(q_n, \epsilon/2^n)$ – Tim kinsella May 10 at 2:03
• Ah! Very nice. I had been assuming that you were talking about a dense set, for some reason.... – Cameron Buie May 10 at 2:13
• If you change “almost everywhere” to “everywhere” then you could say “does not exist” because you could invoke the intermediate value theorem to conclude that $f(\mathbb{R})$ is uncountable, a contradiction. But the “almost everywhere “ assumption seems challenging. – Michael May 10 at 3:27
• Hmm, I don't see how to take advantage of the fact that $f$ is not injective on any open interval. Any nowhere monotone continuous function is neither injective nor constant on any open interval (examples include the Weierstrauss function or a.s. any realization of Brownian motion). – forgottenarrow May 13 at 20:06

Such a function exists.

To construct it, we need an auxiliary function $$w: \mathbb R \to [0,1]$$ that is

• Continuous.
• Rational almost everywhere.
• Irrational at $$0$$.
• Zero outside the interval $$(-1,1)$$.

This can be obtained from the Cantor function $$c: [0,1] \to [0,1]$$ by taking some $$u \in (0,1)$$ for which $$c(u)$$ is irrational and by assigning $$w(x) = \begin{cases} 0, & x < -u \\ c(x+u), & x \in [-u,0] \\ w(-x), & x > 0 \end{cases}$$ Now we enumerate all rationals as the sequence $$\{r_n\}_{n=1}^{\infty}$$ and look for the required function $$f$$ as the sum of the series $$f(x)=\sum_{n=1}^{\infty} \frac {b_n}{2^n}\,w\!\left(\frac{x-r_n}{k_n}\right),$$ where the sequences $$\{k_n\}_{n=1}^{\infty} \subset \mathbb R^+$$ and $$\{b_n\}_{n=1}^{\infty} \subseteq \{0,1\}$$ are built inductively.

Let $$k_1=1$$, $$b_1=1$$. Then the first term of the series is a continuous function that is rational almost everywhere but is irrational at $$x=r_1$$.

For any $$n>1$$, we take $$k_n = \displaystyle \min{\left(\frac 1 {2^{n+1}}, \min_{i=1}^{n-1} {|r_i-r_n|}\right)}$$ and assign $$b_n=0$$ if the sum of already defined terms of the series gives an irrational number at $$x=r_n$$, or $$b_n=1$$ otherwise. As a result, the $$n$$th term $$f_n(x)=\frac {b_n}{2^n}\,w\!\left(\frac{x-r_n}{k_n}\right)$$ so defined is always a continuous function that is rational almost everywhere, has absolute value not greater than $$\frac 1 {2^n}$$, is nonzero at most at an interval of length $$\frac 1 {2^n}$$ and changes (or keeps) value of $$f(r_n)$$ to an irrational number preserving $$f(r_i)$$ for each $$i.

The sum of the series so defined gives us the required function $$f$$ because:

• The series is uniformly convergent and thus converges to a continuous function.
• The value of $$f(x)$$ at $$x=r_n$$ is assured to be irrational in a finite sum after $$n$$ steps and never changes after this, so $$f$$ is irrational at all rational points.
• Let $$I=\{x \in \mathbb R \mid f(x) \notin \mathbb Q\}$$. Note that for any $$n \in \mathbb N$$: $$I \subseteq \left(\bigcup\limits_{i=1}^{n} Y_i\right) \bigcup \left(\bigcup\limits_{i=n+1}^{\infty} Z_i\right)$$, where $$Y_i=\{x \in \mathbb R \mid f_i(x) \notin \mathbb Q\}$$ and $$Z_i=\{x \in \mathbb R \mid f_i(x) \ne 0\}$$. As noted above, $$\mu(Y_i)=0$$ and $$\mu(Z_i) \le \dfrac 1 {2^i}$$. Hence $$\displaystyle \mu(I) \le \sum_{i=1}^n \mu(Y_i) + \sum_{i=n+1}^{\infty} \mu(Z_i) \le \sum_{i=n+1}^{\infty} \frac 1 {2^i} = \frac 1 {2^n}$$. But $$n$$ is arbitrary, so passing this to the limit as $$n \to \infty$$, we get $$\mu(I)=0$$, i.e. $$f$$ is rational almost everywhere.
• +1 for a nice construction! (It saddens me on the other hand as I spent 2 days on proving the nonexistence of such a function...) – YuiTo Cheng May 14 at 3:44
• What a great solution! So the idea was to first construct a function that's continuous almost everywhere and discontinuous at a point. Then use the countability of the rationals to construct the function! Thanks! – forgottenarrow May 14 at 6:53
• @forgottenarrow Right, except that $w$ is continuous everywhere, including $x=0$ (it is an even function in my example, though it is not required). Without this fact we couldn't argue that the sum of the series is continuous. – John McClane May 14 at 11:17
• Oops, it was late night. I meant to say rational almost everywhere and irrational at a point. – forgottenarrow May 14 at 15:41