Is there a function $f\colon\mathbb{R}\to\mathbb{R}$ such that every non-empty open interval is mapped onto $\mathbb{R}$?

The Conway base 13 function is one such function. From Wikipedia:

$f$ takes as its value every real number somewhere within every open interval $(a,b)$.

The construction of the function is a little bit complicated. Refer to the wiki page for details.


Let $K$ be the Cantor set. For any $a, b\in \mathbb R,$ $a\ne 0,$ I'll also call $aK+b$ a Cantor set. Since $K$ has the cardinality of $\mathbb R,$ so do these other Cantor sets.

Note that if $U$ is any nonempty open subset of $\mathbb R,$ there exists a Cantor set inside of $U.$

Let $I_1,I_2, \dots$ be the open intervals in $\mathbb R$ with rational end points.

Claim: There are pairwise disjoint Cantor sets $K_1,K_2, \dots$ such that for each $n,$ $K_n \subset I_n.$

The claim is a nice exercise. Let's assume the result. For each $n$ there exists a bijection $f_n:K_n \to \mathbb R.$ Define $f:\mathbb R \to \mathbb R$ as follows. For each $n,$ set $f= f_n$ on $K_n.$ Set $f=0$ everywhere else.

Then $f$ has the desired property. Proof: Suppose $I$ is a nonempty open interval. Then $I_n\subset I$ for some $n.$ Since $K_n \subset I_n,$ we have $\mathbb R = f(K_n) \subset f(I_n) \subset f(I),$ and we're done.


My answer to this question is precisely such a function, constructed in an easy way:

There is in fact a rather easy example of a function $\mathbb R \to \mathbb R$ such that the image of every open set is $\mathbb R$: Let $(x_i)_{i\in\mathbb Z_+}$ be the binary decimal expansion of $x$, so that each $x_i\in\{0,1\}$. Let then $$f(x)=\sum_{k=1}^∞\frac{(−1)^{x_k}}k\quad \textrm{if the series converges}$$ $$f(x)=0\quad\textrm{otherwise.}$$ Since the harmonic series (or a tail of it) can be made to converge to any real number by changing signs in the appropriate way, this function has $f((a,b))=\mathbb R$ for any real $a,b$.

The harmonic series can be substituted with any other unbounded series where the summand goes to zero.