(* Generalized Cantor Sets https://twitter.com/wtgowers/status/1170692765342216192 The Cantor set is the set of all real numbers between 0 and 1 that have just 0s and 1s in their ternary expansion (i.e., no 2s). It is what you get if you continue this process of "removing the middle third" to infinity. We can generalize this to think of an integer Cantor set to consist of all integers that have just 0s and 1s in their base-3 representation. In this version, I convert n to base-3 and then check the result digit-by-digit. The answer can be found more efficiently if I check each digit as we generate it, rather than make a second pass. *) function main(n : integer): boolean has_no_2s(to_base3(n)) function to_base3(n : integer): integer if n < 3 then n else 10 * to_base3(n / 3) + MOD(n, 3) function has_no_2s(n : integer): boolean if n < 10 then n < 2 else has_no_2s(n / 10) and has_no_2s(MOD(n, 10)) (* from the Klein library *) function MOD(m : integer, n : integer): integer m - m/n * n