The Cantor ternary set is a remarkable subset of the real numbers named after German mathematician Georg Cantor who described the set in 1883. It has the same cardinality as \(\mathbb{R}\), yet it has zero Lebesgue measure.
The set can be constructed recursively by first considering the unit interval \(C_{0}=[0,1]\). In the next step, we divide the set into three equal parts and discard the open middle set. This leads to the new set \(C_{1}=[0,\tfrac{1}{3}]\cup[\tfrac{2}{3},1]\). This procedure is repeated on the two remaining subintervals \([0,\tfrac{1}{3}]\) and \([\tfrac{2}{3},1]\), and iteratively on all remaining subsets such that \(C_n = \tfrac{1}{3}C_{n-1} \cup (\tfrac{2}{3} + \tfrac{1}{3}C_{n-1})\). The Cantor set is defined by the limit as \(n\to\infty\), i.e., \(\mathcal{C} = \cap_{n=0}^{\infty} C_{n}\).
The recursion can be illustrated in python in the following way
import numpy as np def transform_interval(x, scale=1.0, translation=0.0): return tuple(map(lambda z: z * scale + translation, x)) def Cantor(n): if n==0: return {(0,1)} Cleft = set(map(lambda x: transform_interval(x, scale=1/3), Cantor(n-1))) Cright = set(map(lambda x: transform_interval(x, translation=2/3), Cleft)) return Cleft.union(Cright) ...