# Discontinuous function sending compacts to compacts

I know that the condition that $f(X)$ is compact if $X$ is compact should not be sufficient to say that $f$ is continuous, but I can't come up with an example of such discontinuous $f$. What is it?

Thanks

-

Let $f:\Bbb R\to\Bbb R$ be such that $f(x)=0$ if $x\le 0$ and $f(x)=1$ if $x>0$.

-
Beat me to it..... – user38268 Jun 11 '12 at 7:05
Me too. We all think of the same counterexamples. – Alex Becker Jun 11 '12 at 7:06
Except Asaf, who had to get fancy. :-) – Brian M. Scott Jun 11 '12 at 7:08
@BrianM.Scott 4 upvotes in the space of 2 minutes, a record to beat :D – user38268 Jun 11 '12 at 7:08
@AlexBecker Well that was kinda the obvious one to go for :D :D – user38268 Jun 11 '12 at 7:08

You can take $f\colon\mathbb {R\to R}$ to be $f(x)=\begin{cases}0 & x\in\mathbb Q\\ 1 & x\notin\mathbb Q\end{cases}$

This function is discontinuous everywhere but its image is a finite set and therefore compact.

-

More generally, if you let $K$ be any compact subset of $\mathbb R$ with at least two points, pick some $x_0\in K$ and let $f:\mathbb R\to\mathbb R$ defined by $$f(x)=\begin{cases}x & x\in K\\ x_0 & x\notin K\end{cases}$$ then $f$ is a discontinuous function with image $K$.

-
If we take $K$ to be a compact set which is nowhere dense, but has a positive measure (copies of a fat Cantor set in every $[k,k+1]$, for example) then we can get such function $f$ which is discontinuous on a set of positive measure, which is interesting. – Asaf Karagila Jun 11 '12 at 7:33
Surely not in every $[k,k+1]$? That set would be unbounded. – Johan Jun 11 '12 at 7:57
@Johan: True. However if we make this union disjoint we have a closed set that is still good enough for our purposes. – Asaf Karagila Jun 11 '12 at 8:06