Functions that are continuous only at two points?

I need to find a function $f:\mathbb{R}\to\mathbb{R}$ which is continuous only at two points, but discontinuous everywhere else.

How on earth would I go about doing this? I can't think of any function like this.

Thanks in advance.

Edit: I've seen examples including the indicator function for rationals. Is this the only method of finding such functions?


Solution 1:

Think of something like $$f(x)=\begin{cases} x^2 & \text{if } x \in \mathbb{Q}\\ x & \text{if } x \in \mathbb{R-Q}\\ \end{cases} $$ This is only continuous at two points, namely where $x^2=x$.

Solution 2:

The following function is a standard example of a function that is continuous at one point only: $$ f(x) = \begin{cases} x &: x \in \Bbb Q \\ 0 &: \text{ otherwise.}\end{cases} $$

Show that this is indeed the case. Can you think of a way to modify it so that it's continuous at two points? Hint:

Instead of $x$ in the first case, consider the polynomial $(x - x_0)(x - x_1)$.

This can be generalized to construct a function that is continuous at a finite set of points only.

Solution 3:

What about $$ f(x) = \begin{cases} x(1-x) & x \in \mathbb{Q} \\ 0 & x \notin \mathbb{Q} \\ \end{cases}? $$