# Testing a description of the Cantor ternary set

The Cantor ternary set (aka the Cantor discontinuum) is, as usual, the set $K = \bigcap_{n=0}^{\infty} K_{n}$ where $K_{0} = [0, 1]$, the closed unit interval, and where for each $n \geq 1$ the set $K_{n}$ is obtained by removing from $K_{n-1}$ the open middle third of each of the closed intervals whose union is $K_{n-1}$.

I believe that $K_{n}$ may be described as consisting of those real numbers $x$ for which at least one of $3x - 0$ or $3x - 2$ belongs to $K_{n-1}$. (Compare the analogous description of the Menger sponge in the Wikipedia article https://en.wikipedia.org/wiki/Menger_sponge.) I want to check this description with Mathematica.

Here's a realization of that description, employing predicates:

inKQ[0][x_] := 0 <= x <= 1
inKQ[n_][x_] := Or @@ inKQ[n-1] /@ (3 x - {0, 2})


Thus:

inKQ[1] /@ {4/9, 5/9, 1/3, 2/3, 0, 1}
(* {False, False, True, True, True, True} *)

inKQ[2] /@ {0, 1, 1/3, 2/3, 4/9, 5/9, 1/27, 4/27, 20/27, 22/27}
(* {True, True, True, True, False, False, True, False, True, False} *)


So far, so good: that is evidence that accords with the mathematical definition of $K_{1}$ and $K_{2}$.

How might I then use Mathematica to verify, say for $n = 1, 2, 3, 4$ — and, preferably, for general positive integers $n$ — that my description of $K_{n}$ is correct?

To answer this question requires, of course, a different description in Mathematica of the $n$th set $K_{n}$. One start would be to obtain the endpoints of $K_{n}$, as in:

Flatten @ Rationalize @ MeshCoordinates[CantorMesh[2]]
(* {0, 1/9, 2/9, 1/3, 2/3, 7/9, 8/9, 1} *)


But I don't quite see yet how to exploit that to do the check I want.

You can use the fact that elements of the Cantor set, expressed in base 3, can only have a 1 in the last digit (to allow for endpoints). To handle nlevels of the construction we only look at the first up to n+1 such digits, and discard the last one.

inCantorQ[n_, val_] :=
FreeQ[Most[Take[RealDigits[val, 3][[1]], UpTo[n + 1]]], 1]

Table[
inCantorQ[2,
j], {j, {0, 1, 1/3, 2/3, 4/9, 5/9, 1/27, 4/27, 20/27, 22/27}}]

(* Out[489]= {True, True, True, True, False, False, True, False, True, \
False} *)


Bigger test:

len = 30;
n = 4;
rats = RandomInteger[{0, 2}, {len, n + 1}].(1/3^Range[1, n + 1])

(* Out[507]= {80/243, 160/243, 227/243, 1/3, 23/81, 167/243, 79/81, \
224/243, 83/243, 236/243, 101/243, 52/81, 92/243, 13/243, 145/243, \
22/27, 38/81, 13/243, 212/243, 40/81, 76/243, 64/81, 238/243, 2/81, \
80/243, 77/243, 23/81, 83/243, 50/81, 241/243} *)

in4 = Map[inCantorQ[n, #] &, rats]

(* Out[511]= {True, False, False, True, False, False, True, True, False, \
True, False, False, False, False, False, False, False, False, False, \
False, False, False, False, True, True, False, False, False, False, \
True} *)


Consistency check:

in4 === inKQ[n] /@ rats

(* Out[512]= True *)

• OK! I was hoping to avoid explicit ternary expansions and rely instead on the more usual "geometric" description of the Cantor set by removing open middle-thirds. Another way might be an explicit description $K_0=[0, 1]$ and $K_n = \frac{1}{3} K_{n-1} \cup \left(\frac{2}{3} + \frac{1}{3} K_{n-1}\right)$ where the addition and the multiplication by the scalar are done elementwise; but unclear how best to represent that recursive version in Mathematica. Aug 12, 2018 at 21:10
• I don't see anything wrong with your proposed geometric approach. I was trying to provide something suitable for the desired "different description...of the nth set". But I do not know if it can be adapted to validate your code, as opposed to validating individual instances. Aug 12, 2018 at 22:17