Ones and Twos for days

Inspiring myself on a recent challenge, we ought to compute a sequence that is very close to A160242.

Task

Your task is to generate the sequence $$\ \{s_i\}_{i=0}^\infty \$$:

1, 2, 1, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, ...

Which is more easily understandable in this format:

      1 2 1
1 2 2 2 1
1 2 2 2 2 2 1
1 2 2 2 2 2 2 2 1 ...


Another way to think of it is, this sequence is the concatenation of blocks $$\b_i, 0 \leq i\$$ where block $$\b_i\$$ is a 1, followed by $$\2i + 1\$$ 2s, followed by another 1.

Input

If your program takes input, the input is a non-negative integer n, telling you how far you should go in computing the sequence.

The sequence can

• be 0-indexed, so that $$\s_0 = 1, s_1 = 2, s_2 = 1, ... \$$
• be 1-indexed, so that $$\s_1 = 1, s_2 = 2, s_3 = 1, ... \$$

Output

Your code may do one of the following:

• indefinitely print the sequence
• print/return the term n as given by the input
• print/return all the terms up to the term n as given by the input

Test cases

(the test cases are 0-indexed)

0 -> 1
1 -> 2
2 -> 1
3 -> 1
4 -> 2
5 -> 2
6 -> 2
7 -> 1
8 -> 1
9 -> 2
10 -> 2
11 -> 2
12 -> 2
13 -> 2
14 -> 1
15 -> 1
16 -> 2
17 -> 2
18 -> 2
19 -> 2
20 -> 2
21 -> 2
22 -> 2
23 -> 1
24 -> 1
25 -> 2
26 -> 2
27 -> 2
28 -> 2
29 -> 2


This is so the shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!

• If I do my answer in 1-index, can I treat the test cases as 1-indexed as well? – Jeff Zeitlin Mar 13 '20 at 17:14
• @JeffZeitlin of course! – RGS Mar 13 '20 at 17:15
• You should leave the IO formats as the default rather than override them. If you meant to re-iterate the defaults, then you left a couple out (like a few of the options for functions) – Jo King Mar 14 '20 at 1:51
• @JoKing I was pretty confident I had seen a fair share of sequence challenges where the 3 standard outputs were these. What did I leave out? – RGS Mar 14 '20 at 6:33
• I may be taking indefinitely print the sequence  too literally, but the tag wiki for sequence allows returns an infinite lazy iterator/generator – Jo King Mar 14 '20 at 10:51

APL+WIN, 31 bytes

Prompts for integer. Index origin=1
Returns single term of the series.

2 1[1++/m=1,(n+1),n←+\1+2×⍳m←⎕]


Try it online! Courtesy of Dyalog Classic

Haskell, 51 Bytes

f n=concat['1':['2'|x<-[0..y*2]]++"1"|y<-[0..n]]!!n

• Not very competitive, but I thought that I would provide a Haskell solution. – Benji Mar 13 '20 at 20:10

Wolfram Language (Mathematica), 26 bytes

Count[√#+√(#+1),_@__]&


Try it online!

1-indexed.

√#+√(#+1)           (* sqrt(n)+sqrt(n+1) *)
Count[ % ,_@__]     (* count nonatomic subexpressions at the first level. *)


Haskell, 31 bytes

do n<-[1..];show\$div(100^n)9*11


Try it online!

32 bytes

show=<<iterate(\x->x*100+121)121


Try it online!