Ok. This question may sound very easy, but actually i am in great need of it. I have been facing trouble in constructing functions, which are only continuous at some particular sets.
For e.g, the standard example of a function which is only continuous at one point, is the function, $f(x) = x, \ x \in \mathbb{Q}$ and $f(x) = -x, x \in \mathbb{R} \setminus \mathbb{Q}$. Similarly, i would like to know as to how to construct a function which is
Continuous at exactly $2,3,4$ points.
Continuous exactly at integers
Continuous exactly at Natural numbers
Continuous exactly at Rationals.
I would like to see many examples (with proof!), so that i can don’t struggle when somebody asks me to construct such functions.
One simple way of constructing a function which is continuous only at a finite number of points, $x=a_1,\ldots,a_n$, is to do a slight modification to the function you give: take a polynomial $p(x)$ that has roots exactly at $x=a_1,\ldots,a_n$ (e.g., $p(x) = (x-a_1)\cdots(x-a_n)$) , and then define
$$ g(x) = \left\{\begin{array}{ll}
p(x) & \text{if $x\in\mathbb{Q}$;}\\
0 & \text{if $x\notin\mathbb{Q}$.}
\end{array}\right.$$
The function is continuous at $a_1,\ldots,a_n$, and since $p(x)\neq 0$ for any $x\notin\{a_1,\ldots,a_n\}$ then $g(x)$ is not continuous at any point other than $a_1,\ldots,a_n$. Other possibilities should suggest themselves easily enough.
A function that is continuous exactly at the integers: a similar idea will work: find a function that has zeros exactly at the integers, for example $f(x)=\sin(\pi x)$, and then take
$$g(x) = \left\{\begin{array}{ll}
\sin(\pi x) & \text{if $x\in\mathbb{Q}$;}\\
0 & \text{if $x\notin\mathbb{Q}$.}
\end{array}\right.$$
A function continuous exactly in the natural numbers: take a function that is continuous at the integers, and redefine it as the characteristic function of the rationals in appropriate places(what happens at $0$ depends on whether you believe $0$ is in the natural numbers or not). Assuming that $0\in\mathbb{N}$, one possibility is:
$$g(x) = \left\{\begin{array}{ll}
\sin(\pi x)&\text{if $x\in\mathbb{Q}$ and $x\geq 0$;}\\
x & \text{if $x\in\mathbb{Q}$ and $-\frac{1}{2}\lt x\leq 0$;}\\
1 & \text{if $x\in\mathbb{Q}$ and $x\leq -\frac{1}{2}$;}\\
0 & \text{if $x\notin\mathbb{Q}$.}
\end{array}\right.$$
A function continuous exactly on the rationals. This one is a bit trickier. There is no such function. This follows because the set of discontinuities of a real valued function must be a countable union of closed sets.
Perhaps then, we might anticipate the next question:
A function that is continuous exactly on the irrationals. An example is the following: let $s\colon\mathbb{N}\to\mathbb{Q}$ be an enumeration of the rationals (that is, a bijection from $\mathbb{N}$ to $\mathbb{Q}$. Define $f(x)$ as follows:
$$f(x) = \sum_{\stackrel{n\in\mathbb{N}}{s_n\leq x}} \frac{1}{2^n}.$$
The function has a jump at every rational, so it is not continuous at any rational. However, if $x$ is irrational, let $\epsilon\gt 0$. Then there exists $N$ such that $\sum_{k\geq N}\frac{1}{2^k}\lt \epsilon$. Find a neighborhood of $x$ which excludes every $q_m$ with $m\leq N$, and conclude that the difference between the value of $f$ at $x$ and at any point in the neighborhood is at most $\sum_{k\geq N}\frac{1}{2^k}$.
Edit: As I was reminded in the comments by jake, in fact the “standard example” of a function that is continuous at every rational and discontinuous at every rational is Thomae’s function. The example I give is a monotone function, and although it is discontinuous at every rational, it is continuous from the right at every number.
Continuous at 2, 3, 4: $f(x)=(x-2)(x-3)(x-4)$ if $x$ is rational, $f(x)=0$ if $x$ is irrational.
Continuous at the integers: $f(x)=\sin(\pi x)$ if $x$ is rational, 0 if $x$ is irrational.
Continuous at the natural numbers: $f(x)=\sin(\pi x)$ if $x$ is rational and not a nonpositive integer, 0 if $x$ is irrational, 1 if $x$ is a nonpositive integer.
Continuous exactly at the rationals: Impossible, because the set of rational numbers is not a $G_\delta$.