Topic

Cantor sets and dimension

The middle-thirds Cantor set is the numbers in [0, 1] with a base-3 expansion using only the digits 0 and 2. Whether a fraction belongs to it is a question with a definite answer, and the library answers it exactly.

const C = cantor();                         // base 3, digits 0 and 2
C.contains("1/4");                           // { inside: true, expansion: "0.#02" }: 0.020202… in base 3
C.contains("1/2");                           // { inside: false, … }
C.stage(3);                                  // the 8 intervals of the third stage, exactly
C.measure(10);                               // 1024/59049
C.dimension();                               // log 2 / log 3, certified
cantor({ base: 10, digits: [0, 1, 2, 3, 4, 5, 6, 7, 8] });   // decimals without a 9

Membership is decidable

x is in the set exactly when some allowed digit d leaves 3x−d3x - d in [0, 1] and in the set again. For a rational x=n/Dx = n/D every number reached this way has the same denominator D, and there are only D + 1 such numbers in [0, 1]. So the steps form a finite graph, and x is in the set exactly when the graph has an infinite path from x, which means a path that reaches a cycle. contains searches for one and returns the repeating expansion it found. A number can have two expansions (1/3 is 0.1000… and also 0.0222…), and the search finds the allowed one when there is one.

The answer is not a matter of precision. 1/4 is in the Cantor set although it is never an end point of a removed interval, and 1/2 is not, although every stage contains points as close to it as you like.

Stages, measure and dimension

Stage n is the union of knk^n intervals of length b−nb^{-n}, where k is the number of allowed digits; touching intervals are merged. The total length is (k/b)n(k/b)^n, which tends to 0 whenever a digit is missing, so the set has measure zero. Its dimension is log⁡k/log⁡b\log k / \log b: the set is made of k copies of itself scaled by 1/b1/b. That is exact when rational (base 4 with two digits gives 1/2) and a certified interval otherwise.

The expansion of any rational in any base, with its repeating part marked by #, comes from Core: C.expansion("1/4").

callreturns
cantor({ base = 3, digits = [0, 2] })A DigitCantor
contains(q){ inside, expansion?, reason }
expansion(q)Core's base-b expansion, with # before the repeating part
stage(n), measure(n)[lo, hi] rational pairs; (k/b)^n
dimension(){ kind, value, interval, formula }
Try itEdit the code and run it with @ratmath/chaos and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs