Non-zero smooth functions vanishing on a Cantor set

A Cantor set $C\subset[0,1]$ is closed, and that is all we need. Therefore $f(x)=d(x,C)$ (distance from $x$ to $C$) vanishes on and only on $C$. It is also Lipschitz-continuous.

For your third question, you can take $g(x)=f(x)^2$. This is $C^1$ apart from peaks in the middle points of the intervals of the complement of $C$. You can easily smooth the function near these points, since you are well away from $C$.

Choosing a higher power gives you any $C^k$ smoothness you want. But you can also get $C^\infty$. Let $\phi:\mathbb R\to[0,\infty)$ be a smooth function vanishing on $(-\infty,0]$ and set $g(x)=\phi(f(x))$ and mollify the tips.

Conclusion: The answer to all three questions is yes.

Edit: It is not entirely clear what you mean by constructing by hand. This is as close as I can come to making an explicit function if the Cantor set is not specified. (I also noted that the distance was also used in this answer to the OP's linked question. But it was not concerned with regularity.)


By a theorem of Whitney, for any closed subset $C$ in $\mathbb R^n$ there exists a nonnegative $C^\infty$-function $f$ on $\mathbb R^n$ which vanishes exactly at $C$. This also holds on any smooth manifold. I tried to find the exact reference, but I could not find it just now. Maybe, this result is hidden in:

  • Whitney, Hassler: Analytic extensions of differentiable functions defined in closed sets, Trans. AMS 36 (1934), 63--89.

It's not hard to construct such a function explicitly. Let $$\psi(x) = \begin{cases} e^{-1/(1-x^2)}, & -1 < x < 1 \\ 0, & \text{else} \end{cases}$$ so that $\psi$ is $C^\infty$ and is nonzero precisely on $(-1,1)$. In the usual middle-thirds construction of the Cantor set, let $C_n$ be the centers of the intervals removed at step $n$ (there are $2^{n-1}$ of them and they have width $3^{-n}$; computing these points explicitly is left as an exercise for the reader with nothing better to do). Then let $$f(x) = \sum_{n=1}^\infty \sum_{y \in C_n} \frac{1}{n!} \psi(2 \cdot 3^n(x-y)).$$ It should be easy to verify that the sum converges uniformly with all its derivatives, so $f$ is also $C^\infty$, and by construction we have made it strictly positive precisely on the removed intervals, i.e. the complement of the Cantor set.