<s>[INST] Direct formula for sequence 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.
 [/INST] The following PARI/GP code will do what you want
a(n) = (n-3*2^(k=exponent((n-1)\3+1))+2)\2^k+1;

Note that exponent(n) is the binary exponent of $n$. In other words, the floor of the base $2$ logarithm of $n$. Also, n\m is the floor of $n/m$. Here is an example of use of the code:
? vector(45,n,a(n))
[1,2,3,1,1,2,2,3,3,1,1,1,1,2,2,2,2,3,3,3,3,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3]

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.
</s>