I want to find out the function to generate the sequence with the following pattern.
1 2 3 1 1 2 2 3 3 1 1 1 1 2 2 2 2 3 3 3 3 ....
Where the lower bound number is 1 upper number bound number is 3. Each time numbers start from 1 and each number repeats 2 ^ n times, with n starting with 0.
Direct formula for sequence
224 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 2 best solutions below
On
Im not sure if you want a closed form solution or not. The following solution is programmable with while loops
Let $$ 1,2,3,1,1,2,2,3,3,...=a_1 , a_2, a_3, a_4, ... $$ Define $n_0 = 0$ to be the initiation of the sequence. The sequence resets to $1$ every so often (for example, it resets at position $4$, then again at position $10$). The $k^{th}$ reset, $k \geq 1$ occurs at position $$n_k = 1+3 \sum_{j=0}^{k-1}2^j$$ The length of the patterns also get bigger. The $k^{th}$ iteration of the pattern is of length $\ell_k = 3 (2^k)$ for $k\geq 0$.
Then we can define 2 functions. The first function $f(n)$ identifies which iteration of the pattern we are in: $$ f(n) = \min\{ k : n_{k} \leq n < n_{k+1} \} $$ And the second function $g(x)$ is what you want. It decides where in the iteration we are (if we are in the first third of the iteration, return 1. If in the second, return 2, if in the third return 3): $$ g(n) = \begin{cases} 1 & \mbox{ if }\,\, 0 \leq \frac{n - n_{f(n)}}{\ell_{f(k)}} < \frac{1}{3} \\ 2 & \mbox{ if }\,\, \frac{1}{3} \leq \frac{n - n_{f(n)}}{\ell_{f(k)}} < \frac{2}{3} \\ 3 & \mbox{ if }\,\, \frac{2}{3} \leq \frac{n - n_{f(n)}}{\ell_{f(k)}} < 1 \end{cases} $$
The following PARI/GP code will do what you want
Note that
exponent(n)is the binary exponent of $n$. In other words, the floor of the base $2$ logarithm of $n$. Also,n\mis the floor of $n/m$. Here is an example of use of the code:The logic is not hard. We need $\lfloor\log_2(n)\rfloor+1$ for something like $[1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,\dots]$. Because we repeat 3 times in each cycle we need to replace $n$ with something like $\lfloor n/3\rfloor$. The actual number we need is $$k = \lfloor\log_2(\lfloor (n-1)/3\rfloor +1)\rfloor. $$ This ensures that $\, 0 \le n - 3\cdot 2^k +2 < 3\cdot 2^k .\,$ Note that $\, 1 \le (n - 3\cdot 2^k + 2)/2^k + 1 < 4.$ which is what we want.