# S[α] for strings of ordinals

Update 2020/03/10: Added complexity analysis for S.substr(α, N)

In a previous blog post, I defined for each ordinal $\alpha$ a string $S_\alpha$ (made of the characters for the empty set, comma, opening brace and closing brace) that enumerates the element of $\alpha$. I gave a simple formulas to calculate the length $L_\alpha$ of this string.

My colleague Ioanna was a bit disappointed that I didn’t provide a script for calculating the infinite $S_\alpha$ strings. Obviously, the complexity would be “$\Omega\left(\omega\right)$” but it is still possible to evaluate the string at a given position: Given $\beta < L_\alpha$, what is the character of $S_\alpha$ at position $\beta$?

Since the initial segments of the strings are compatible, another way to express this is by introducing the class $S = \bigcup_\left\{\alpha \in \left\{\mathrm\left\{Ord\right\}\right\}\right\} S_\alpha$ corresponding to a giant string enumerating the class $\mathrm\left\{Ord\right\}$ of all ordinals. Given an ordinal $\alpha$, what is $S\left[\alpha\right]$?

A small generalization of this S.charAt(α) operation is S.substr(α, N) calculating the substring of length $N$ starting at $\alpha$.

## Example

• $S_\left\{2\right\}$ is "∅,{∅}", so $S\left[0\right]$ is the character ∅, $S\left[1\right]$ is a comma, $S\left[2\right]$ is an opening brace and $S\left[4\right]$ is a closing brace.
• $S_\left\{\omega+1\right\}$ is made of of an $\omega$-concatenation of finite strings (the character ∅, a comma, $S_1$ surrounded by braces, a comma, $S_2$ surrounded by braces, a comma, $S_4$ surrounded by braces, a comma, etc), followed by a comma, an opening brace, the same $\omega$-concatenation of finite strings and finally a closing brace. So $S\left[\omega\right]$ is a comma, $S\left[\omega+1\right]$ is an opening brace, $S\left[\omega+2\right]$ is the character "∅" and $S\left[\omega2\right]$ is a closing brace.

In the previous example, we have basically analyzed the string $S_\left\{\alpha+1\right\}$ at a given successor ordinal, splitting it into two copies of $S_\alpha$, comma and braces. This suggests some easy values of $S$:

## Lemma

For any $n < \omega$ and $\beta \geq 1$, $S\left[\alpha\right]$ is:

• A comma if $\alpha$ can be written $2^n - 3$ or $\omega^\beta 2^n + n$
• An opening brace if $\alpha$ can be written $2^n - 2$ or $\omega^\beta 2^n + n + 1$
• A closing brace if $\alpha$ can be written $2^\left\{n+1\right\} - 4$ or $\omega^\beta 2^\left\{n+1\right\} + n$

Proof: For any ordinal $\alpha$, by viewing the string $S_\left\{\alpha+1\right\}$ as a concatenation of $S_\alpha$, a comma, an opening fence, $S_\alpha$ and a closing fence, we deduce that:

• $\left\{S\left\left[L_\left\{\alpha\right\}\right\right]\right\}$ is a comma.
• $\left\{S\left\left[L_\left\{\alpha\right\} + 1 \right\right]\right\}$ is an opening brace.
• $L_\left\{\alpha+1\right\}$ is a successor ordinal and $\left\{S\left\left[L_\left\{\alpha+1\right\} - 1 \right\right]\right\}$ is a closing brace.

The lemma follows immediately from the calculation of string lengths performed in the previous blog post. □

Warning: The rest of the blog post gives the solution to this puzzle, so you might want to have fun solving it yourself first and then go back checking my proposed solution later 😉...

More generally, the proof of the lemma can be extended by saying that if we find $n$ such that $\left\{2^n - 1\right\} \leq \alpha \leq \left\{2^\left\{n+1\right\} - 5\right\}$ then $\delta = \left\{\alpha - \left\{\left(2^n - 1\right)\right\}\right\} \leq \left\{2^n - 4\right\}$ is the index of $\left\{S\left[\alpha\right]\right\}$ in the second substring $S_\alpha$ of $S_\left\{\alpha+1\right\}$ and so $\left\{S\left[\alpha\right]\right\} = \left\{S\left[\delta\right]\right\}$.

Details will be provided in the theorem below but one can already write a simple JavaScript recursive program to evalute $S$ at finite ordinals:

## Script for $\alpha < \omega$

The character of $S$ at position $\alpha =$

is

The following intermediary step will be helpful to evaluate $S$ at infinite ordinal $\alpha$:

## Proposition

Let $\alpha$ is infinite and $\beta = \log_\left\{\omega\right\}\left(\alpha\right) \geq 1$. Let $1 \leq q < \omega$ and $0 \leq \rho < \omega^\beta$ be the quotient and remainder of the euclidean division of $\alpha$ by $\omega^\beta$. Let’s define:

Then $S\left[\alpha\right]$ is:

• A comma if $\alpha = \left\{\omega^\left\{\beta\right\} 2^n + n\right\}$
• An opening brace if $\alpha = \left\{\omega^\left\{\beta\right\} 2^n + n + 1\right\}$
• An closing brace if $\alpha = \left\{\omega^\left\{\beta\right\} 2^\left\{n+1\right\} + n\right\}$
• $S\left[\delta\right]$ otherwise where $\delta$ is the unique ordinal such that $\alpha = \left\{\omega^\beta 2^n + n + 2 + \delta\right\}$ and $\delta < \left\{\omega^\left\{\beta\right\} 2^n + n\right\}$.

Proof: First by construction we have $\alpha = \left\{ \left\{\omega^\beta q \right\} + \rho\right\}$.

If the first case of the definition of $n$, $2^n \leq q < 2^\left\{n+1\right\}$ so we always have $\alpha < \left\{\omega^\beta \left\{\left(q+1\right)\right\}\right\} \leq \omega^\beta 2^\left\{n+1\right\} \leq L_\left\{\omega\left\{\beta\right\} + n + 1\right\}$. If additionnaly $q$ is not a power then $2^n < q$ and so $\alpha \geq \left\{\omega^\left\{\beta\right\}q\right\} \geq \left\{\omega^\left\{\beta\right\}\left\{\left(2^n+1\right)\right\}\right\} \geq L_\left\{\omega\left\{\beta\right\} + n\right\}$. Otherwise $q = 2^n$ and $n \leq \rho$ and so again $\alpha \geq L_\left\{\omega\left\{\beta\right\} + n\right\}$.

In the second case of the definition of $n$, $\left\lfloor \left\{\log_2\left(q\right)\right\} \right\rfloor \geq \rho + 1$ so $n \geq \rho \geq 0$. $q$ is a power of 2 and more precisely $q = 2^\left\{n+1\right\} > 2^n$ so we deduce the same way as in the previous case that $\alpha \geq L_\left\{\omega\left\{\beta\right\} + n\right\}$. Moreover, $\rho < n + 1$ so again $\alpha = \left\{\omega^\beta 2^\left\{n+1\right\} + \rho\right\} < L_\left\{\omega\left\{\beta\right\} + n + 1\right\}$.

We can thus view $S_\left\{\omega \beta + n + 1\right\}$ as a concatenation of $S_\left\{\omega \beta+n\right\}$, a comma, an opening brace, $S_\left\{\omega \beta+n\right\}$ and a closing brace. We assume that $\left\{ \left\{\omega^\left\{\beta\right\} \left\{2^n\right\}\right\} + n + 2\right\} \leq \alpha < \left\{\omega^\left\{\beta\right\} 2^\left\{n+1\right\} + n\right\}$ as the three other cases are handled by the lemma. Then $\delta$ is well-defined and is actually the index of $S\left[\alpha\right]$ in the second copy of $S_\left\{\omega \beta + n\right\}$ so $\left\{S\left[\alpha\right]\right\} = S\left\{\left[\delta\right]\right\}$. □

We are now ready to give a nice way to evaluate $S$ at any ordinal $\alpha$:

## Theorem

$S\left[\alpha\right]$ can be calculated inductively as follows:

• If $\alpha = 0$, $S\left[\alpha\right]$ is the character "∅".
• If $0 < \alpha \leq \omega$ then $\right\rfloor\right\}\right\} - 3\right\} \leq \alpha \leq \right\rfloor + 1\right\}\right\} - 4\right\}$ and $S\left[\alpha\right]$ is equal to:
• A comma if $\alpha = \left\{2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\} - 3\right\}$
• An opening brace if $\alpha = \left\{2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\} - 2\right\}$
• A closing brace if $\alpha = \left\{2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor + 1\right\}\right\} - 4$
• $S\left[\delta\right]$ otherwise where $\delta = \left\{\alpha - \left\left( 2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\} - 1 \right\right)\right\} \leq \left\{2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\} - 4\right\} \leq \alpha - 3$.
Moreover $\delta$ compares against $\alpha$ as follows: $\frac\left\{\delta\right\}\left\{\alpha\right\} \leq \frac\left\{1\right\}\left\{2\right\} + \frac\left\{1\right\}\left\{2^\left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor + 1\right\}\right\} \leq \frac\left\{3\right\}\left\{4\right\}$ and $\left\{\left\lfloor \left\{\log_2\left\{\left(\delta+3\right)\right\}\right\} \right\rfloor\right\} < \left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\}$
• If $\alpha$ is infinite, let $0 < c_1 < \omega$ be the coefficient in its Cantor normal form corresponding to the smallest infinite term and $c_0 < \omega$ be finite term if there is one, or zero otherwise. Let $k$ be the exponent corresponding to the smallest nonzero term in the binary decomposition of $c_1$. Then $S\left[\alpha\right]$ is equal to:
• A closing brace if $c_0 \leq k - 1$.
• A comma if $c_0 = k$.
• An opening brace if $c_0 = k + 1$.
• $S\left[\left\{c_0 - \left\{\left(k+2\right)\right\}\right\}\right]$ if $c_0 \geq k + 2$.

Proof:

The case $\alpha = 0$ is clear. Suppose $0 < \alpha < \omega$ and let $l = \left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\}$. By definition $2^l \leq \alpha + 3 < 2^\left\{l+1\right\}$ so $2^l - 3 \leq \alpha \leq 2^\left\{l+1\right\} - 4$. Let’s consider the case $2^l - 1 \leq \alpha \leq 2^\left\{l+1\right\} - 5$ as the three other cases are already known from the Lemma. $S_\left\{l+1\right\}$ is made of the concatenation of $S_l$, a comma and $S_l$ surrounded by braces. $\delta$ is actually the index of $S\left[\alpha\right]$ in the second copy of $S_l$ so $\left\{S\left[\alpha\right]\right\} = S\left\{\left[\delta\right]\right\}$. The first equality is straighforward:

$\delta = \left\{\alpha - \left\{\left(2^l -1\right)\right\}\right\} \leq \left\{2^\left\{l+1\right\} - 5 - \left\{\left(2^l -1\right)\right\}\right\} = \left\{2^l - 4\right\} = \left\{2^l - 1 - 3\right\}\leq \alpha - 3$

Morever we have:

$\frac\left\{\delta\right\}\left\{\alpha\right\} \leq \left\{1 - \frac\left\{2^l - 1\right\}\left\{2^\left\{l+1\right\}+5\right\}\right\} \leq \left\{1 - \frac\left\{2^l - 1\right\}\left\{2^\left\{l+1\right\}\right\}\right\} \leq \left\{\frac\left\{1\right\}\left\{2\right\} + \frac\left\{1\right\}\left\{2^\left\{l+1\right\}\right\}\right\}$

and so the second inequality follows from the fact that $l \geq 1$. Finally, the third inequality comes from:

$2^\left\{\left\lfloor \left\{\log_2\left\{\left(\delta+3\right)\right\}\right\} \right\rfloor\right\} \leq \left\{\delta + 3\right\} \leq \left\{2^l - 4 + 3\right\} < 2^l$

Let’s consider the case of an infinite $\alpha$. For some $N \geq 1$, $\beta_N > \beta_\left\{N-1\right\} > \dots > \beta_1 \geq 1$ and $c_0 < \omega$ and $0 < c_1, c_2, c_N < \omega$, we can write Cantor’s Normal form as:

$\alpha = \left\{ \omega^\left\{\beta_N\right\} c_N\right\} + \left\{ \omega^\left\{\beta_\left\{N-1\right\}\right\} c_\left\{N-1\right\} \right\} + \dots + \left\{ \omega^\left\{\beta_\left\{1\right\}\right\} c_\left\{1\right\} \right\} + c_0$

With the notation of the proposition, we have $\beta = \beta_N$, $q = c_N$ and

$\rho = \left\{ \omega^\left\{\beta_\left\{N-1\right\}\right\} c_\left\{N-1\right\} \right\} + \dots + \left\{ \omega^\left\{\beta_\left\{1\right\}\right\} c_\left\{1\right\} \right\} + c_0$

If $N \geq 2$, then $\rho$ is infinite and so $n = \left\lfloor \left\{\log_2\left(q\right)\right\} \right\rfloor$ and we are in the fouth bullet of the proposition. Moreover $q \geq 2^n$ and we can write:

$\alpha = \left\{ \omega^\left\{\beta\right\} 2^n\right\} + \left\{\omega^\left\{\beta\right\} \left\left(q-2^n\right\right)\right\} + \rho = \left\{ \omega^\left\{\beta\right\} 2^n\right\} + n + 2 + \delta$

where $\delta = \left\{\omega^\left\{\beta\right\} \left\left(q-2^n\right\right)\right\} + \rho$ is infinite and so cancels out the $n + 2$ term. It follows that $S\left\{\left[\alpha\right]\right\} = S\left\left[ \left\{\omega^\left\{\beta\right\} \left\left(q-2^n\right\right)\right\} + \rho \right\right]$. Essentially, we have just removed from $c_\left\{N\right\}$ its term of highest exponent in its binary decomposition!

By repeated application of the theorem, we can remove each binary digit of the $c_i$ for $i$ going from $N$ to $2$. When then arrive at $i = 1$:

$\left\{S\left[\alpha\right]\right\} = S\left\left[ \omega^\left\{\beta_1\right\} c_1 + c_0\right\right]$

With the notation of the proposition, we now have $\beta = \beta_1$, $q = c_1$ and $\rho = c_0$. If the binary decomposition of $c_1$ has more than one nonzero digit then so $q$ is not a power of 2. So although $\rho$ is now finite, we are still in the first case of the proposition and $\delta$ remains infinite. So we can remove all but the last digit of $c_1$ by repeated application of the proposition:

$\left\{S\left[\alpha\right]\right\} = S\left\left[ \omega^\left\{\beta_1\right\} 2^k + c_0\right\right]$

where $k$ is the exponent corresponding to the smallest nonzero term in the binary decomposition of $c_1$.

Using the lemma, $S\left[\alpha\right]$ is a comma if $k = c_0$, an opening brace if $k = c_0 - 1$ and a closing brace if $k = c_0 + 1$.

If $k \leq c_0 - 2$ then $k = \left\lfloor \left\{\log_2\left(2^k\right)\right\} \right\rfloor \leq c_0$ and writing

$\left\{\omega^\left\{\beta_1\right\} 2^k + c_0\right\} = \omega^\left\{\beta_1\right\} 2^k + k + 2 + \left\left(c_0 - \left\{\left(k + 2\right)\right\}\right\right)$

we deduce from the proposition that $\left\{S\left[\alpha\right]\right\} = \left\{S\left[\left\{c_0 - \left\{\left(k+2\right)\right\}\right\}\right]\right\}$.

Finally, if $k \geq c_0 + 2$ then $k = \left\lfloor \left\{\log_2\left(2^k\right)\right\} \right\rfloor > c_0$ and writing

$\left\{\omega^\left\{\beta_1\right\} 2^k + c_0\right\} = \left\{\omega^\left\{\beta_1\right\} 2^\left\{k-1\right\}\right\} + k - 1 + 2 + \left\{\omega^\left\{\beta_1\right\} 2^\left\{k-1\right\}\right\} + c_0$

we deduce from the proposition that

$\left\{S\left[\alpha\right]\right\} = S\left\left[ \omega^\left\{\beta_1\right\} 2^\left\{k-1\right\} + c_0\right\right]$

We have essentially decremented $k$ and we can repeat this until we reach the case $k = c_0 + 1$ for which we already said that the character is a closing brace. □

As an application of this theorem, here is a few simple exercises:

## Exercise 1

1. $S\left\left[2^72 + 2\right\right]$ is an empty set.
2. $S\left\left[\omega^72\right\right]$ is a comma.
3. $S\left\left[\omega^72 72 + 72\right\right]$ is a closing brace.
4. $S\left\left[\omega^72 72 + \omega^42 42 + 12\right\right]$ is an opening brace.

## Exercise 2

The evaluation of $S$ at

## Corollary 1: Time complexity

Let $\alpha$ be an ordinal. Let $c_1 < \omega$ be the coefficient in its Cantor normal form corresponding to the smallest infinite term if there is one, or zero otherwise. Let $c_0 < \omega$ be its finite term if there is one, or zero otherwise. Then $S\left[\alpha\right]$ can be evaluated in:

• $O\left\left( \log_2\left\{\left(c_0+2\right)\right\}^2 + \log_2\left\{\left(c_1+2\right)\right\} \right\right)$ elementary arithmetic operations and comparisons on integers.
• $O\left\left( \log_2\left\{\left(c_0+2\right)\right\}\right\right)$ elementary operations if integers are represented in binary and leading/trailing zero counting and bit shifts are elementary operations.

Proof: First note that the “+ 2” is just to workaround for the edge cases $c_0 = 0$ or $c_1 = 0$.

For the infinite case $c_1 > 0$ we need to calculate $k$ that is performing the find first set. A naive implementation can be done in $O\left(\log_2\left\{\left(c_1+2\right)\right\}\right)$ steps by browsing the digits of $c_1$ to find the first nonzero for example by calculating the remainder modulo increasing power of 2. Each iteration requires only $O\left(1\right)$ elementary integer operations %, * and comparisons. Then returning the result of moving to the finite case requires $O\left(1\right)$ integer operations +, − and comparisons.

For the finite case $c_1 = 0$ and $\alpha = c_0$, first notice that we only require $O\left\left( \log_2\left\{\left(c_0+2\right)\right\}\right\right)$ recursive calls given the inequality:

$\left\{\left\lfloor \left\{\log_2\left\{\left(\delta+3\right)\right\}\right\} \right\rfloor\right\} < \left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\}$

The case $\alpha = 0$ only requires one comparison. For the case $\alpha > 0$, we need to calculate $l = \left\{\left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor\right\}$ which is integer rounding of the binary logarithm or even just $2^l$. As above, we can provide a naive implementation by calculating the quotient modulo increasing power of 2 in $O\left(l\right)$ comparisons and elementary integer operations /, *. Then returning the result of moving to a $\delta < \alpha$ requires only $O\left(1\right)$ integer operations and comparisons. In total, complexity is $O\left\left( \log_2\left\{\left(c_0+2\right)\right\}^2\right\right)$.

Finally, this can be simplified if one calculates $k$ and $l$ by a simple leading/trailing zero counting (or similar) and $2^l$ by a bit shift. □

## Script based on Cantor normal form

If the coefficients of Cantor normal form of $\alpha$ corresponding the smallest infinite term and finite term are
$c_1$ =
$c_0$ =
Then the character of $S$ at position $\alpha$ is

Once we have an algorithm for S.charAt(α), it is easy to get an algorithm of $N$ times that complexity for S.substr(α, N) calculating the substring of length $N$ starting at position $\alpha$ by repeated calls to S.charAt(α). Let’s analyze a bit more carefully how we can make this recursive and more efficient:

## Corollary 2: Algorithm for S.substr(α, N)

Let $\alpha$ be an ordinal and $N < \omega$. Then S.substr(α, N) can be calculated as follows:

• If $N = 0$ then it is the empty string.
• If $\alpha = 0$ and $N = 1$ then it is the character "∅".
• If $0 < \alpha < \omega$, let $l = \left\lfloor \left\{\log_2\left\{\left(\alpha+N+2\right)\right\}\right\} \right\rfloor$. We have $2^l-3 \leq \left\{\alpha + N - 1\right\} \leq 2^\left\{l+1\right\} - 4$ and the result is obtained by concatenating the following strings:
1. If $\alpha \leq 2^l - 4$, the substring at offset $\alpha$ and length $2^l - 3 - \alpha$.
2. If $\alpha \leq \left\{2^l - 3\right\}$, a comma.
3. If $\alpha \leq \left\{2^l - 2\right\} \leq \left\{\alpha + N - 1\right\}$, an opening brace.
4. If $\left\{\alpha + N - 1\right\} \geq \left\{2^l - 1\right\}$, the substring at offset $\delta = \mathrm\left\{max\right\}\left\{\left(0, \alpha - \left\{\left(2^l - 1\right)\right\}\right)\right\}$ and length $1 + \mathrm\left\{min\right\}\left\{\left(\left\{\alpha + N - 1\right\}, \left\{2^\left\{l+1\right\} - 5\right\}\right)\right\} - \mathrm\left\{max\right\}\left\{\left(\alpha, \left\{\left(2^l - 1\right)\right\}\right)\right\}$.
5. If $\left\{\alpha + N - 1\right\} = \left\{2^\left\{l+1\right\} - 4\right\}$, a closing brace.
• If $\alpha$ is infinite, let $0 < c_1 < \omega$ be the coefficient in its Cantor normal form corresponding to the smallest infinite term and $c_0 < \omega$ be finite term if there is one, or zero otherwise. Let $k$ be the exponent corresponding to the smallest nonzero term in the binary decomposition of $c_1$. Then the result is obtained by a concatenating the following strings:
1. If $c_0 \leq k - 1$, $\left\{\mathrm\left\{min\right\}\left\{\left(N, k - c_0\right)\right\}\right\}$ closing braces.
2. If $c_0 \leq k \leq c_0 + N - 1$, a comma.
3. If $c_0 \leq k + 1 \leq c_0 + N - 1$, an opening brace.
4. If $c_0 + N - 1 \geq k + 2$, the substring at offset $\delta = \mathrm\left\{max\right\}\left\{\left(\left\{0, \left\{c_0 - \left\{\left(k + 2\right)\right\}\right\}\right\}\right)\right\}$ and length $c_0 + N - \left\{\mathrm\left\{max\right\}\left\{\left(c_0, k + 2\right)\right\}\right\}$.

Moreover, this only adds $O\left(N\right)$ compared to the complexity of evaluating to a single offset.

Proof: The algorithm is just direct application of the Theorem. For the case where $\alpha \geq \omega$, the only change is that we add at most $N$ characters before moving to the finite case. The case $\alpha < \omega$ is essentially a divide-and-conquer algorithm and we have a relation of the form:

$\left\{T\left(L\right)\right\} = 2\left\{T\left\left(\frac\left\{L\right\}\left\{2\right\}\right\right)\right\} + \left\{f\left(L\right)\right\}$

where $L = 2^l = \left\{\Theta\left\{\left(\alpha + N\right)\right\}\right\}$ and $f\left\{\left(L\right)\right\}$ is $O\left(1\right)$ or $O\left(\log_2\left\{L\right\}\right)$ depending on available operations, but in any case $O\left\{\left(\sqrt\left\{L\right\}\right)\right\}$. So from the master theorem, $\left\{T\left\{\left(L\right)\right\}\right\} = \left\{O\left(L\right)\right\} = \left\{O\left(\alpha+N\right)\right\}$. In general, this bound is not as good as repeating $N$ calls to S.charAt!

However, we note that if we assume that $\alpha > N - 4$ then $\left\{\alpha + 2 + N\right\} < \left\{2\left\{\left(\alpha+3\right)\right\}\right\}$ and so

$\log_2\left\left(\alpha + 2 + N\right\right) < \left\{1 + \log_2\left\{\left(\alpha+3\right)\right\}\right\}$

We can easily discard the edge case where these start/end offsets point to a brace surrounding the right substring of the iterative step and so we get:

$\left\lfloor \left\{\log_2\left\{\left(\alpha+N+2\right)\right\}\right\} \right\rfloor = \left\lfloor \left\{\log_2\left\{\left(\alpha+3\right)\right\}\right\} \right\rfloor$

which means that the left substring is just empty and so the complexity is not changed compared to S.charAt. Finally, when $\alpha \leq N - 4$ the previous bound tells us that the steps are done in $O\left(N\right)$. □

## Script for S.substr(α, N)

If the coefficients of Cantor normal form of $\alpha$ corresponding the smallest infinite term and finite term are
$c_1$ =
$c_0$ =
Then the character of $S$ substring of length $N$ =
and offset $\alpha$ is