### Cantor set by Mathematica

#### by Yi Zhang

Essentially one can not really show the complete Cantor set, here by its definition is what’s left after first n remove of “one-third” open sets. The Mathematica script is then:

Cantor[x__, n_] := Nest[Join[Partition[#[[All, 1]], 1], Partition[#[[All, 1]] + (#[[All, 2]] - #[[All, 1]])/3, 1], 2] \[Union] Join[Partition[#[[All, 1]] + 2 (#[[All, 2]] - #[[All, 1]])/3, 1], Partition[#[[All, 2]], 1], 2] &, x, n]

Now

Cantor[{{0, 1}}, 2]

gives

{{0, 1/9}, {2/9, 1/3}, {2/3, 7/9}, {8/9, 1}}

where the first argument is the interval to be sliced, and the second argument gives the level (iteration times) of generation. The output is the intervals left after the remove action. Try

Cantor[{{0, 1}}, 3]

we have

{{0, 1/27}, {2/27, 1/9}, {2/9, 7/27}, {8/27, 1/3}, {2/3, 19/27}, {20/ 27, 7/9}, {8/9, 25/27}, {26/27, 1}}

and

Cantor[{{0, 1}}, 4] {{0, 1/81}, {2/81, 1/27}, {2/27, 7/81}, {8/81, 1/9}, {2/9, 19/ 81}, {20/81, 7/27}, {8/27, 25/81}, {26/81, 1/3}, {2/3, 55/81}, {56/ 81, 19/27}, {20/27, 61/81}, {62/81, 7/9}, {8/9, 73/81}, {74/81, 25/ 27}, {26/27, 79/81}, {80/81, 1}}

and

Cantor[{{0, 1}}, 5] {{0, 1/243}, {2/243, 1/81}, {2/81, 7/243}, {8/243, 1/27}, {2/27, 19/ 243}, {20/243, 7/81}, {8/81, 25/243}, {26/243, 1/9}, {2/9, 55/ 243}, {56/243, 19/81}, {20/81, 61/243}, {62/243, 7/27}, {8/27, 73/ 243}, {74/243, 25/81}, {26/81, 79/243}, {80/243, 1/3}, {2/3, 163/ 243}, {164/243, 55/81}, {56/81, 169/243}, {170/243, 19/27}, {20/27, 181/243}, {182/243, 61/81}, {62/81, 187/243}, {188/243, 7/9}, {8/9, 217/243}, {218/243, 73/81}, {74/81, 223/243}, {224/243, 25/27}, {26/ 27, 235/243}, {236/243, 79/81}, {80/81, 241/243}, {242/243, 1}}

Plotting could be done using

PlotCantor[{x1_, x2_}] := Plot[x = 1, {x, x1, x2}, Filling -> Bottom, PlotRange -> {{0, 1}, {0, 1}}, Axes -> {True, False}] Show[PlotCantor /@ Cantor[{{0, 1}}, 2]] Show[PlotCantor /@ Cantor[{{0, 1}}, 4]]

Advertisements