$E \subseteq \mathbb{R}^n$ is compact $\iff f(E)$ is compact, then f is continuous

calculuscompactnesscontinuitygeneral-topologymetric-spaces

Let $f:R^n \to R^m$ such that for every $E \subseteq \mathbb{R}^n, E$ is compact $\iff f(E)$ is compact, then f is continuous.
I saw this proof on MSE:

"Suppose $f$ is not continuous at $x$. Then there exists $r>0$ and $x_n \to x$ such that $|f(x_n)-f(x)| \geq r$ for all $n$. Now, Since the graph is compact, $(x_n,f(x_n))$ has a subsequence $(x_{n_k}, f(x_{n_k}))$ converging to some point $(u,f(u))$ on the graph. This implies $x_n\to u$ so we must have $u=x$ and $f(x_{n_k}) \to f(u)=f(x)$. But $|f(x_{n_k})-f(x)| \geq r$ for all $k$ and we have arrived at a contradiction."

But does this proof hold for my claim? This proof does not use the iff assumption, just one direction rather: $E$ is compact $\implies f(E)$ is compact. However, we can find a counterexample for the following claim:
For every $E \subseteq \mathbb{R}^n,$ the set $f(E) \subseteq \mathbb{R}^m$ is also compact (this time not iff) then f is continuous.
Indeed, consider the function $f: \mathbb{R}\rightarrow \mathbb{R}$ defined by $f(x)=1$ if $x \in \mathbb{Q}$ and $f(x)=0$ otherwise. Then $f$ is nowhere continuous, but the image of each compact subset $K$ of $\mathbb{R}$ is compact as finite sets are compact.
I am a bit confused. Any help would be greatly appreciated.

Best Answer

The $n$ in this proof has nothing to do with the dimension of the space! Sorry for this mixup. Suppose $x_n$ are distinct points converging to $x$. By omitting first few terms we may suppose $x\neq x_n$ for any $n$. We want too show that $f(x_n) \to f(x)$. Since $\{x,x_1,x_2,...\}$ is compact so is its image. Hence, $(f(x_n))$ is bounded. It is enough to show that this bounded sequence has $f(x)$ as its only limit point. Suppose $y$ is a limit point. Then, since $\{f(x),f(x_1),f(x_2),...\}$ is compact, hence closed, we see that $y=f(x)$ or $y=f(x_n)$ for some $n$. Suppose $y=f(x_n)$ for some $n$. Then $f(x_n)$ is a limit point, so some subsequence $f(x_{n_j})$ converges to $f(x_n)$. The set $\{f(x_n), f(x_{n_1}), f(x_{n_2}),f(x_{n_3}),...\}$ is compact and this implies $\{x_n, x_{n_1}, x_{n_2},x_{n_3},...\}$ is compact. But then $x$ ( a limit point of the set) must be in this set. This contradicts the fact that $x\neq x_n$ for any $n$.

Related Question