Continuity of Un-plottable Piece-wise Defined Function

I'm working on the following problem:

Consider the function $$f(x) = \begin{cases} x + x^2 & x \in \mathbb{Q} \\ x - x^2 & x \notin \mathbb{Q} \\ \end{cases} $$ Prove that $f$ is nowhere continuous except at $x=0$.

My attempt:

First, we show that $f$ is continuous at $x=0$. Accordingly, fix $\epsilon > 0$, and choose $\delta < \frac{-1 + \sqrt{1 + 4 \epsilon}}{2}$ (note that since the quantity on the left hand side is strictly positive, the Archimedean Principle guarantees the existence of such a $\delta$). Then, if $$ |x - 0| = |x| < \delta$$ then $$ |f(x) - f(0)| = |f(x)| = \begin{cases} |x + x^2| & x \in \mathbb{Q} \\ |x - x^2| & x \notin \mathbb{Q} \\ \end{cases} $$ In either case, the triangle inequality gives that \begin{align*} |f(x)| &\leq |x| + |x|^2 \\ &< \delta + \delta^2 \\ &= \frac{-2 + 2\sqrt{1+4 \epsilon} + 1 - 2\sqrt{1 + 4 \epsilon} + 1 + 4 \epsilon}{4} \\ &= \frac{4\epsilon}{4} \\ &= \epsilon \end{align*} Thus, for arbitrary $\epsilon>0$, $\exists \delta > 0$ s.t. $$ |x - 0| < \delta $$ implies $$ |f(x) - f(0)| < \epsilon,$$ so $f$ is continuous at $x=0$. Now, consider some real $x_0 \neq 0$. It follows from the Sequential Density of Irrationals/Rationals that there exists some sequence $u_n$ of rational numbers that converges to $x_0$, and there exists some sequence $v_n$ of irrational numbers that also converges to $x_0$. Now, since the limits $\lim_{n \rightarrow \infty} u_n = x_0$ and $\lim_{n \rightarrow \infty} v_n = x_0$ exist, we conclude that

$$\lim_{n \rightarrow \infty} f(u_n) = \lim_{n \rightarrow \infty} u_n + u_n^2 = x_0 + x_0^2$$ and $$\lim_{n \rightarrow \infty} f(v_n) = \lim_{n \rightarrow \infty} v_n + v_n^2 = x_0 - x_0^2.$$ Since these limits are distinct for $x_0 \neq 0$, it follows that either the image of $u_n$ or the image of $v_n$ under $f$ does not converge to $f(x_0)$. Thus, $f$ is not continuous for any $x_0 \neq 0$.

Is this a valid proof? I'm concerned I jumped in a bit too eagerly - is it necessary to include the former proof of continuity? Or does it suffice to say the images limit to the same value only when $x_0$ is identically 0?


The example given to you generalizes to what is a great class of problems posed in the topic of continuity, to students in their first year or so(and in plenty of competitive exams, including one I had seen recently). That is,

Let $f,g : \mathbb R \to \mathbb R$ be two continuous functions. Define for $S \subset \mathbb R$ the indicator function $1_S : \mathbb R \to \{0,1\}$ by $1_S(x) = 1$ if $x \in S$ and $0$ otherwise. Define the function $h(x) = f1_{\mathbb Q} + g1_{\mathbb R \setminus \mathbb Q}$. Then $h$ is continuous precisely at the points when $f = g$.

For example, in the situation you had above, we have $f = x+x^2$ and $g = x - x^2$, and these are equal exactly at $x = 0$.

The proof is very simple. Indeed, at points where $f(x) \neq g(x)$, we may approach via the rationals and irrationals separately(as you did in the second part of your proof) to get discontinuity at $x$, since $f(x)$ and $g(x)$ will be the limits of the values of the rational and irrational sequences respectively (by continuity) and these are unequal at the point.

However, if $f(x) = g(x)$ then fix $\epsilon > 0$. Since $\lim_{t \to x} f(t) = \lim_{t\to x} g(t) = f(x) = g(x)$, there are $\delta_1$ and $\delta_2$ corresponding to $f$ and $g$, and we may take the minimum of these to get a $\delta > 0$, which will work out.

A common example of this is with $f \equiv 0$ and $g \equiv 1$, the characteristic function of the rationals which is continuous nowhere.