# A question about non-compact metric spaces.

I noticed the following statement on a comprehensive exam, and I am having trouble proving it. Can anyone help?

If $X$ is a non-compact metric space, then there exists a continuous function $f: X \rightarrow \mathbb{R}$ such that $f(X)$ is dense in $\mathbb{R}$.

This seems not true because $f:\mathbb{N}\longrightarrow \mathbb{R}$ can not have $F(\mathbb{N})$ dense in $\mathbb{R}$. Notice that $\mathbb{N}$ is not compact with the topology induced by usual metric. – user29999 Sep 3 '12 at 15:36
@user29999 Fix an enumeration of the rationals $r_n$ define a map $f: \mathbb N \rightarrow \mathbb R$ by $f(n)=r_n$. Since $\mathbb N$ has the discrete topology any map with it as the domain is continuous. Thereby $f$ is a continuous map with dense image. This is also a step in Nate's hint. – JSchlather Sep 3 '12 at 15:43
Hint: If $X$ is not compact, it contains an infinite set $E$ with no limit point. Try defining $f : E \to \mathbb{R}$ so that $f(E)$ is dense. Then show that you can extend $f$ continuously to $X$.