A formula for length of representation of a number in a "base" without zeros

49 Views Asked by At

If you had 2 items the sequence would go like this: $$1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5, \ldots$$

This is $\lfloor\log_2(n+2)\rfloor$.

What if I wanted to do for 3 items which goes like this: $$1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,\ldots$$

A visual representation for $3$ items as $(A,B,C)$:

f(0) = 1:  A ──┐
f(1) = 1:  B   ├─ 3^1
f(2) = 1:  C ──┘
f(3) = 2:  AA ─┐
f(4) = 2:  AB  │
f(5) = 2:  AC  │
f(6) = 2:  BA  │
f(7) = 2:  BB  ├─ 3^2
f(8) = 2:  BC  │
f(9) = 2:  CA  │
f(10) = 2: CB  │
f(11) = 2: CC ─┘
f(12) = 3: AAA ┐
f(13) = 3: AAB │
f(14) = 3: AAC │
f(15) = 3: ABA │
f(16) = 3: ABB ├─ 3^3
f(17) = 3: ABC │
f(18) = 3: ACA │
f(19) = 3: ACB │
f(20) = 3: ACC │
...
1

There are 1 best solutions below

2
On BEST ANSWER

Hint The number of elements of length $k$ is $3^k$, so the number of elements of length $\leq k$ is $$3 + 3^2 + \cdots 3^k = \frac{3}{2} (3^k - 1) .$$ Thus, the $n$th element in the sequence $A, B, C, AA, AB, \ldots$ has length $k$ iff $$\tfrac{3}{2} (3^{k - 1} - 1) < n \leq \tfrac{3}{2}(3^k - 1) .$$

Rearranging gives that the $k$ for which the previous inequalities hold is precisely $$\left\lceil \log_3 \left(\tfrac{2}{3} n + 1\right) \right\rceil .$$ Since the indexing in the question begins with $0$, i.e, since $a$ is the $(a + 1)$st number in the list, the function $f$ is $$f(a) := \left\lceil \log_3 \left(\tfrac{2}{3} (a + 1) + 1\right) \right\rceil .$$ It is straightforward to generalize this to arbitrary numbers of symbols $(A, B, C, \ldots)$.