Function defined everywhere but continuous nowhere

I have been recently reading about the Weierstrass function, a function that is continuous everywhere but differentiable nowhere. It made me think of a similar puzzle involving functions: find $$f: \mathbb R \to \mathbb R$$ such that $$f$$ can be computed anywhere, is well defined, but is continuous nowhere.

I first thought of maybe mapping the reals on to a fractal and doing something with that point but that’s just a fuzzy idea and I doubt one could compute it everywhere. In my research I could find no such function that is defined for all real numbers, both rational and irrational. If anyone has a proof this is impossible (or even just an idea of how you might prove that), or an example of a function that has those properties, that would be great.

• We were challenged in high school by our math teacher to ask a question he couldn't answer outright and in third grade when we were learning derivatives I asked for a function that is continuous everywhere but differentiable nowhere and he couldn't answer it :D He was a marvelous teacher but didn't care that much for maths higher than our level. – chx Oct 24 at 23:55
• @chx Is this a copypasta? – Don Thousand Oct 25 at 2:06
• No, this is my life. I am still proud and thought it's funny. Sorry if not. – chx Oct 25 at 6:19
• I'm assuming by third grade, you mean the third year of high school. Otherwise, you don't live on planet earth – Don Thousand Oct 30 at 22:27

First off, the "majority" of functions (where majority is defined properly) have this property, but are insanely hard to describe. An easy example, though, of a function $$f:\mathbb R\to\mathbb R$$ with the aforementioned property is $$f(x)=\begin{cases}x&x\in\mathbb Q\\x+1&x\notin\mathbb Q\end{cases}$$This example has the added benefit of being a bijection!

• Get an upvote for "This example has the added benefit of being a bijection!", you made my day! :) – Olivier Roche Oct 23 at 13:42
• Wait, but is this function computable? How do you compute if $x\notin \mathbb Q$? – eyeballfrog Oct 24 at 3:52
• @eyeballfrog I don't think OP means computable in the technical sense. – Don Thousand Oct 24 at 4:10
• And in fact all computable functions (in the technical sense) are continuous everywhere. – TonyK Oct 24 at 12:38
• I apologize, I did not know “computable function” was a technical term. This answer fits what I meant by my question, even though it may not fit what I technically asked. – connor lane Oct 25 at 15:37

Consider the function $$f:\mathbb{R} \rightarrow \mathbb{R}$$ defined by

$$f(x) = \begin{cases} 1, ~~ x \in \mathbb{Q} \\ 0, ~~ x \not\in \mathbb{Q} \end{cases}$$

Now let $$x \in \mathbb{R}$$. Then there exists a sequence $$(x_n)_{n \in \mathbb{N}}$$ with $$x_n \rightarrow x$$ which is entirely contained in $$\mathbb{Q}$$ and a sequence $$(y_n)_{n \in \mathbb{N}}$$ with $$y_n \rightarrow x$$ which is entirely contained in $$\mathbb{R} \setminus \mathbb{Q}$$. Then both sequences converge to $$x$$, however the images of the elements in the sequence converge to $$1$$ and $$0$$, respectively.

• Wow! This is brilliantly simple. I’m kind of disappointed I couldn’t find this myself. – connor lane Oct 23 at 13:38

G. Chiusole's & Olivier's example is the standard one.

In fact, there are functions $$\Bbb R \to \Bbb R$$ that are not only discontinuous at every point but spectacularly so: More precisely, there are functions $$f : \Bbb R \to \Bbb R$$ for which $$f(I) = \Bbb R$$ for every (nonempty) open interval $$I$$ no matter how small; thus in a sense they are as far from being continuous as possible. (Functions with this property are called strongly Darboux functions.) The classic example is the Conway base $$13$$ function:

The Conway base $$13$$ function is a function $$f : \Bbb R \to \Bbb R$$ defined as follows. Write the argument $$x$$ value as a tridecimal (a "decimal" in base $$13$$) using $$13$$ symbols as 'digits': $$0, 1, \ldots, 9, \textrm{A}, \textrm{B}, \textrm{C}$$; there should be no trailing $$\textrm{C}$$ recurring. There may be a leading sign, and somewhere there will be a tridecimal point to separate the integer part from the fractional part; these should both be ignored in the sequel. These 'digits' can be thought of as having the values $$0$$ to $$12$$, respectively; Conway originally used the digits "$$+$$", "$$-$$" and "$$.$$" instead of $$\textrm{A}, \textrm{B}, \textrm{C}$$, and underlined all of the base $$13$$ 'digits' to clearly distinguish them from the usual base $$10$$ digits and symbols.

• If from some point onward, the tridecimal expansion of $$x$$ is of the form $$\textrm{A} x_1 x_2 \cdots x_n \textrm{C} y_1 y_2 \cdots$$, where all the digits $$x_i$$ and $$y_j$$ are in $$\{0, \ldots, 9\}$$, then $$f(x) = x_1 \cdots x_n . y_1 y_2 \cdots$$ in usual base $$10$$ notation.
• Similarly, if the tridecimal expansion of $$x$$ ends with $$\textrm{B} x_1 x_2 \cdots x_n \textrm{C} y_1 y_2 \cdots$$, then $$f(x) = -x_1 \cdots x_n . y_1 y_2 \cdots$$
• Otherwise, $$f(x) = 0$$.

You can get a whole bunch of functions like this (and some with even worse properties!) by inspecting the decimal representation of a number. To make sure these functions are well defined, we'll consider the decimal expansion of a terminating decimal to always end with $$00...$$ rather than the other possibility of ending in $$99...$$. The condition of continuity at non-terminating decimals $$x$$ means precisely that, for any bound $$\varepsilon$$, there is some $$N$$ such that every number $$x'$$ with the same first $$N$$ digits as $$x$$ has $$f(x)-f(x') < \varepsilon$$ (and, indeed, if $$f(x)$$ was also non-terminating, we can replace the $$\varepsilon$$ by a similar condition of agreement of digits). The case where $$x$$ is a terminating decimal is slightly different and annoying, so I won't talk about it.

As a starter, we can define a function $$f(x)$$ that writes $$x$$ in decimal, then counts how many $$9$$'s it has. If the count is finite, $$f(x)$$ is the count. If the count is infinite, $$f(x)=-1$$. This is discontinuous everywhere because knowing that $$x$$ and $$x'$$ share $$N$$ digits for any $$N$$ can, at best, tell you that they share some finite number of $$9$$'s - but the function takes into account every $$9$$ and we have no control after some point in the decimal expansion.

We can make the previous example somewhat worse by choosing a bijection $$k:\{-1,0,1,2,\ldots\}\rightarrow \mathbb Q$$ and then considering $$k\circ f$$ which now, one can check, has the property that the image of any open set is dense in $$\mathbb R$$. That's not very continuous at all!

Another fun example, along a similar line, would be to define $$f(x)$$ to be the number of places after the decimal point that the last $$9$$ in the representation of $$x$$ appears - or $$-1$$ if there are infinitely many $$9$$'s. You could even do worse and let $$f(x)$$ be the $$-1$$ if there are infinitely many $$9$$'s. If there is a last $$9$$, erase all the digits prior to it, leaving an infinite sequence of digits in $$\{0,1,\ldots,8\}$$. Write $$0.$$ before this sequence and interpret it in base $$9$$. Now, the image of every open set is $$[0,1]$$. That's pretty nasty. If you choose a bijection between $$[0,1]$$ and $$\mathbb R$$, now the image of every open set is $$\mathbb R$$.

There's also some examples that people actually do care about. For instance, there's a thing called irrationality measure which basically asks "How hard is this number to approximate by rationals?" The irrationality measure of $$x$$ is defined as the infimum of the $$\mu$$ such that $$0 < \left|x - \frac{p}q \right| < \frac{1}{q^{\mu}}$$ for infinitely many coprime pairs of integers $$(p,q)$$. This might be infinite, but you can always fix it up by mapping $$\infty$$ to some real number. This is $$1$$ at every rational, $$2$$ at algebraic irrationals, and can be anything at least $$2$$ elsewhere. This is actually useful as a tool for showing that things like that are like Liouville numbers (but not quite as extreme) are irrational - but the image of every open set is $$\{1\}\cup [2,\infty]$$, so a pretty nasty function.

Also: bonus, if you take any continuous function and add it to any discontinuous everywhere function, you get a discontinuous everywhere function - and if you take a discontinuous everywhere function and multiply it by a non-zero constant, it is still discontinuous everywhere. It turns out that, in the grand scheme of things, if you choose a function at random, the probability that it is continuous is $$0$$ - it's like randomly choosing a point on a plane and hoping that it lies on a line, except that instead of a "plane" you have an infinite dimensional space which is way bigger than the line.

• Bonus exercise: You can "improve" one of the examples to get a function $f$ with the property that, for open set $U$ and for any $x\in\mathbb R$, the set of $y\in U$ for which $f(y)=x$ is in bijection with $\mathbb R$. – Milo Brandt Oct 23 at 14:15

There is a very simple example, the characteristic function of $$\mathbb{Q}$$, defined as follows :
$$f : x \mapsto \left\{ \begin{matrix} 1 & \textrm{if } x \in \mathbb{Q} \\0 & \textrm{otherwise} \end{matrix} \right.$$

Consider the function $$f:\mathbb{R} \rightarrow \mathbb{R}$$ defined by $$f(x)=\begin{cases}x&x\in\mathbb Q\setminus\{0\}\\ -x&x\notin\mathbb Q \\ \sqrt{3}&x=0 \end{cases}$$

This function is not continuous for any $$x\in\mathbb R$$. Suppose $$x_0 \neq 0,$$ then by taking a sequence of rational numbers converging to $$x_0$$ and then a sequence of irrational numbers converging to $$x_0$$, you can see that $$\lim_{x\to{x_0}}f(x)$$ doesn't exist. As zero is a rational number, it is also a bijection.

• The title of the cited paper is "The Blancmange Function Continuous Everywhere but Differentiable Nowhere" (emphasis mine). – Xander Henderson Nov 17 at 19:06
• @XanderHenderson: Yes, I misread the question and have updated my answer. – Axion004 Nov 17 at 21:37