g | x | w | all
Bytes Lang Time Link
025Wolfram Language Mathematica201015T170812Zatt
010Husk201015T160219ZRazetime
024Python 2200314T015032Zxnor
031Haskell201015T193332Zxnor
036Haskell201015T180328Zlynn
016MAWP 0.0200622T051224ZDion
021cQuents200409T033145ZPkmnQ
017dirt verbose mode200409T032150Zcardboar
037Python 3200409T062117ZDion
075C clang200316T213043ZChristia
027Perl 6200314T014617ZJo King
nanPerl 5200315T153553Zandytech
055PowerShell200314T163651ZWasif
033Symja200314T043726Zlyxal
007W200314T044352Zuser9206
016GolfScript200314T034409Zuser9206
018dc200314T010814ZMitchell
011Charcoal200314T010108ZNeil
056C gcc200313T160721ZS.S. Ann
041Bash200313T164356ZNoodle9
050C gcc lm200313T165842ZNoodle9
00605AB1E200313T214657ZKevin Cr
051Haskell200313T200219ZBenji
031APL+WIN200313T190637ZGraham
067APL Dyalog Unicode200313T180942ZJeff Zei
031Python 3.8 prerelease200313T161331ZSurculos
023PowerShell200313T181114ZAdmBorkB
072Brainfuck200313T180246Zvityavv
034Python 3200313T162904ZRGS
052Python 2200313T162846ZAlec
042Wolfram Language Mathematica200313T161320ZZaMoC
023JavaScript ES7200313T154712ZArnauld
025R200313T155411ZRobin Ry
006Jelly200313T155629Zhyper-ne

Wolfram Language (Mathematica), 26 25 bytes

Count[√#|√+++#,_@__]&

Try it online!

1-indexed.

      √#|√+++#          sqrt(n),sqrt(n+1)
Count[        ,_@__]    how many aren't perfect squares?

Husk, 14 12 10 bytes

ṁ`Jḋ3MRİ12

Try it online!

Infinite list.

-1 byte from Dominic Van Essen.

-2 bytes from Zgarb using voodoo magic.

Python 2, 24 bytes

lambda n:2-(-n%n**.5<.5)

Try it online!

Outputs the \$n\$'th value one-indexed.

We use an arithmetic expression to identify indices n that are either perfect square or one below a perfect square. To do so, we use a measure that's roughly of the distance between n and the next perfect square, given by -n%n**.5.

For squares, we have -n%n**.5==0 because n is an even multiple of its square root, that is for n=k*k, -(k*k)%k==0. If n is one less than a perfect square, then the remainder is <0.5, if it's two less, it's be between 0.5 and 1, and so on. So, the condition -n%n**.50.5 accepts only perfect squares and numbers one less.

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!

Haskell, 36 bytes

do n<-[0..];'1':([1..n]>>"22")++"21"

Try it online!

This expression evaluates to an infinite string.

MAWP 0.0, 16 bytes

[1:![2:1A]%1:2M]

cQuents, 21 bytes

_+(Tr$)%1)=Tr_+$)%1))

Try it online!

Port of the jelly answer.

Explanation

    r$)     r_+$)     # Root (defaults to square root)
       %1        %1   # Modulo 1 (only taking the decimal part)
   T     ) T       )  # Ceiling (round up if there is a decimal part)
  (       =         ) # Equal (same as jelly answer)
_+                    # Successor (same as jelly answer)

List output, 15 bytes

1,D2)*_-(k*2),1

Try it online!

Playing with the output a bit, we can golf off 6 bytes.

Instead of outputting 1,2,2,2,1, it outputs 1,[2,2,2],1.

Explanation

1,              # A one
  D2)           # Digits of 2 (which is of course, just 2)
     *_-(k*2)   # Multiplied by k*2-1 (it's multiplying a list)
             ,1 # Another one

dirt (verbose mode), 17 bytes

"1
2
1"|1"
2
2".*

Run as dirt ones_and_twos.dirt -v -i ""

Outputs each element of the sequence on a new line.

Alternatively, if we allow new lines between some but not all elements,

dirt (verbose mode), questionable format, 13 bytes

"121"|1"22".*

prints

121
12221
1222221
122222221
12222222221
1222222222221
122222222222221
12222222222222221
...

Python 3, 37 bytes

i=1
while 1:print('1'+'2'*i+'1');i+=2

Try it online!

C (clang), 80 75 bytes

j,k=1;main(int a,char**b){for(a=atoi(b[1]);j<a;k+=2,j+=k);return(1<j-a)+1;}

Try it online!

This one takes an input N at the terminal and returns the Nth value. This is zero-indexed.

C (gcc), 48 44 bytes

j,k=1;f(a){for(;j<a;k+=2,j+=k);a=(1<j-a)+1;}

Try it online!

Same basic function here, but modified to just be a function to not take the hit from main's arg-list and converting a string to an integer. Switched over to GCC to take advantage of undefined behavior that avoids need for explicit return.

Perl 6, 28 27 bytes

{flat (1,2,2 xx$++*2,1)xx*}

Try it online!

Anonymous codeblock returning a lazy infinite list. I wish I could do 2 xx++$++ or some combination of it, but i can't figure it out.

Perl 5, 35 + 1 (-p) = 36 bytes

$_=(map{(1,2,(2,2)x$_,1)}0..$_)[$_]

Try it online!

Reads input n from stdin. Simplisticly builds a list of the first at least n elements (actually the first 2n+3 elements), then prints the nth element.

Perl 5, 30 + 1 (-p) = 31 bytes

$_=$_+1>(($_+2)**0.5|0)**2?2:1

Smarter and more efficient way of doing it with Arnauld's formula from above (but adjusted to be 0-indexed instead of 1-indexed, because that's how I like it).

Try it online!

PowerShell, 55 bytes

$i=0;1.."$args"|%{$i+=2;write-host "1,$('2,'*$i)1," -n}

Try it online!

Symja, 38 33 bytes

f(x_):=If(Mod(-(x^.5),1)*x<1,1,2)

Y'all can try it here

This is just a port of xnor's (who helped me save 5 bytes) Python 2 answer in Symja. This took a while to generate, as I had to properly understand what the formula was, due to operator precedence. Anyhow, enjoy!

W, 7 bytes

The long length doesn't really matter to me. W doesn't have a square-checking built-in.

φßéW!r♀

Uncompressed:

){Q1m!=r)

Explanation

)         % Increment the input. [input + 1]
 {        % Pair with the input. [input, input + 1]
  Q       % Find the square root.[sqrt(input), sqrt(input + 1)]
   1m     % Modulo 1. (Find the digits after the decimal point.)
     !    % Are the digits 0? (True, it's a square number. Otherwise,
          % it can't be a square number.)
      =r  % Reduce by equality.
        ) % Increment the result.

GolfScript, 16 bytes

Port of Surculose Sputum's Python answer.

0{100*121+.p.}do

Try it online!

dc, 18 bytes

?d2+vd*rvd*-d/2r-p

Try it online!

This uses 0-based indexing, and it computes the nth value in the sequence for index n. Input is on stdin, and output is on stdout.

It computes $$2 -K_{\neq0}\big(\lfloor{\sqrt{n+2}}\rfloor-\lfloor{\sqrt{n}}\rfloor\big),$$ where \$K_{\neq0}\$ is the function that maps 0 to 0, and any non-zero number to 1. (\$K_{\neq0}\$ isn't built into dc, but you can compute it by dividing a number by itself, as long as you're careful with the stack when its argument is 0.)

There may be spurious output on stderr (due to division by 0).

Here's a TIO link to the test suite.

Charcoal, 11 bytes

↷²1W¹«2P1D2

Try it online! Link is to verbose version of code. Prints the infinite sequence. Explanation:

↷²

Change the default direction to downwards, so that everything now prints vertically by default.

1

Output 1 to the canvas.

W¹«

Loop forever.

2

Output 2 to the canvas.

P1

Output 1 to the canvas, but don't move the cursor.

Copy the canvas to STDOUT.

2

Replace the 1 with a 2.

C (gcc), 56 bytes

i,c;main(){main(i--<-2?i=c+=2:putchar(49+(c+~i&&i+3)));}

-1 byte thanks to RGS!
-8 bytes thanks to ceilingcat!

Try it online!

C (gcc), 46 bytes

i;f(n){for(i=0;n--;)printf("%d",i=i*100+121);}

Stretches the rules and only works up to \$n=4\$.

Try it online!

Bash, 64 61 41 bytes

Saved 20 bytes thanks to S.S. Anne!!!

s=2;while :;do echo 1 $s 1;s+=' 2 2';done

Try it online!

Infinitely prints the sequence.

C (gcc) -lm, 57 \$\cdots\$ 52 50 bytes

Saved 2 bytes thanks to ceilingcat!!!

t;s(n){t=sqrt(n);n=n!=t*t;};f(n){n=1+s(n)*s(n+1);}

Try it online!

Returns the nth term in the sequence.

05AB1E, 6 bytes

>‚ŲË>

Port of @HyperNeutrino's Jelly answer, so make sure to upvote him!

Outputs the result for the 1-based input \$n\$.

Try it online or verify all the first \$n\$ outputs.

Printing the infinite sequence is 9 8 bytes (based on the 6-byter above):

∞ü‚Ų€Ë>

Try it online.

Previous 9 bytes variations for the infinite sequence:

Explanation:

>         # Increase the (implicit) input-integer by 1
 ‚        # Pair it with the (implicit) input-integer: [input, input+1]
  Ų      # Check for both whether they're a square (which will be either both falsey,
          # or just one of the two is truthy)
    Ë     # Check if both are equal (so both falsey) (1 if truhy; 0 if falsey)
     >    # And increase that by 1 (2 if truthy; 1 if falsey)
          # (after which the result is output implicitly)

∞         # Push an infinite positive list: [1,2,3,...]
 ü‚       #  Pair each overlapping pair together: [[1,2],[2,3],[3,4],...]
   Ų     # Check for each whether it's a square
     €Ë   # Check for each pair whether they're equal
       >  # Increase it by 1
          # (after which it is output implicitly as result)              

 ∞        # Push an infinite positive list: [1,2,3,...]
Y и       # Repeat a 2 that many times as list: [[2],[2,2],[2,2,2],...]
    δ     # For each inner list:
   X š    #  Prepend a 1: [[1,2],[1,2,2],[1,2,2,2],...]
      €   # For each inner list:
       û  # Palindromize it: [[1,2,1],[1,2,2,2,1],[1,2,2,2,2,2,1],...]
        ˜ # Flatten the list of lists: [1,2,1,1,2,2,2,1,1,2,2,2,2,2,1,...]
          # (after which it is output implicitly as result)

ÅÉ        # Push all odd numbers equal to or less than the given argument,
          # which will be an infinite sequence without argument: [1,3,5,...]
  Å2      # Create for each value a list with that many 2s: [[2],[2,2,2],[2,2,2,2,2],...]
     δ    # For each inner list:
    X .ø  #  Surround it with 1s: [[1,2,1],[1,2,2,2,1],[1,2,2,2,2,2,1],...]
        ˜ # Flatten the list of lists: [1,2,1,1,2,2,2,1,1,2,2,2,2,2,1,...]
          # (after which it is output implicitly as result)

Haskell, 51 Bytes

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

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

APL (Dyalog Unicode), 67 bytes

This answer returns the entire sequence up to the requested value.

f←{(⊃,/1,¨⌽¨1,¨2⍴⍨¨1+2×1-⍨⍳1+⌈⍟⍵)[⍳⍵]}

Try it online!

Decomposition/Explanation:

f←                                     ⍝ assign the name f to
  {                                  } ⍝ a dfn that takes a single argument
                              ⍟⍵       ⍝ The natural logarithm of the argument,
                             ⌈         ⍝ rounded up to the next integer,
                           1+          ⍝ and add 1 (giving N), then
                          ⍳            ⍝ generate the sequence 1..N,
                       1-⍨             ⍝ and subtract 1 from each term (0..N-1)...
                                       ⍝    ⍨ means to swap the arguments to
                                       ⍝    - (the standard subtraction function), so
                                       ⍝    1-⍨N is the same as N-1.
                     2×                ⍝ ... then multiply each term in the sequence by 2,
                   1+                  ⍝ and add 1
                  ¨                    ⍝ then take each term in the sequence, and
               2⍴⍨                     ⍝ generate a vector of that many 2s
                                       ⍝    ⍨ means to swap the arguments to
                                       ⍝    ⍴ the "shape" function. For simple numeric
                                       ⍝    values of A and B, A⍴B means to generate a 
                                       ⍝    vector of A repeats of B.
                                       ⍝ We now have a vector of vectors of 2s.
              ¨                        ⍝ For each of those vectors,
            1,                         ⍝ prepend a 1, then
           ¨                           ⍝ for each vector, 
          ⌽                            ⍝ reverse it (so that the 1 is on the 'back end'), then
       1,¨                             ⍝ for each (now reversed with a 1) vector, prepend a 1, and
     ,/                                ⍝ concatenate all the vectors. For reasons I don't quite
                                       ⍝    understand, this generates an enclosed object, so
    ⊃                                  ⍝ unenclose (disclose) it into a 'real' vector, and
   (                            )      ⍝ treat all that as an object...
                                 [  ]  ⍝ ...which can be subscripted. If the subscript is
                                       ⍝    a single integer, it will return just that item from
                                       ⍝    the array; if it's a vector, it will return all of the
                                       ⍝    requested values, in the order specified by the vector.
                                  ⍳⍵   ⍝ This subscript is a vector, of the integers 1 to the original
                                       ⍝    argument to the function, so it will generate the entire
                                       ⍝    sequence up to the ⍵th term. If I only wanted the single
                                       ⍝    term, I would omit the ⍳, which would give me only the ⍵th term.

FIRST GOLFING (courtesy ngn):

APL (Dyalog Unicode), 65 bytes

f←{⍵↑(⊃,/1,¨⌽¨1,¨2⍴⍨¨1+2×1-⍨⍳1+⌈⍟⍵)}

Try it online!

Explanation of golfing:

[⍳⍵] is the same as [1 2 3 4 5 6 ... ⍵], 
      which is the first ⍵ items of the vector being subscripted.
      That's the same as 
⍵↑    "Take" the first ⍵ items.  Savings, 2bytes.

SECOND GOLFING (my own discovery):

APL (Dyalog Unicode), 63 bytes

f←{⍵↑⊃,/1,¨⌽¨1,¨2⍴⍨¨1+2×1-⍨⍳1+⌈⍟⍵}

Try it online!

Explanation of golfing: The expression that was formerly subscripted does not need to be parenthesized if using ⍵↑ instead of [⍳⍵]. Savings, 2bytes.

Python 3.8 (pre-release), 31 bytes

i=0
while i:=100*i+121:print(i)

Try it online!

Print the sequence infinitely, with a questionable format.


The following version output the first n terms of the sequence:

Python 3, 56 bytes

lambda n:"".join(f"{1:2<{-~i*2}}1"for i in range(n))[:n]

Try it online!

Input: Non-negative integer n

Output: the first n terms of the sequence, concatenated into a string.

Explanation

A different approach using only string formatting instead of @Arnauld's closed-form formula.

Thus f"{1:2<{-~i*2}}1" evaluates to the string "122..21" with i*2+1 characters "2".

PowerShell, 23 bytes

for(){$i++;1;,2*$i++;1}

Try it online!

Prints the sequence indefinitely.

Brainfuck, 72

+++++++[>+++++++<-]>[>+>+<<-]>>+[<.>>+[>+>+<<-]>[<<.>>-]>[<<+>>-]<<<<.>]

Might be able to make it shorter but here's my shot. Prints indefinetly

  1. Initialize first two cells with (ASCII values for) 1 and 2
  2. Start a loop (looping on the 2 cell but it doesn't really matter)
    1. Print the 1
    2. Increase the count, and then copy the count to the next two cells
    3. Use one of the counts to print the twos (decreases the count until 0, then exits loop)
    4. Copy the second copy of the count back to where it was before
    5. Print a final 1

Python 3, 39 34 bytes

i=1
while 1:print(1,*[2]*i,1);i+=2

Prints the sequence infinitely. Thanks to @SurculoseSputum for saving me 5 bytes.

Try it online!

Python 2: 52 bytes

def g(n):return(2,1)[not((n+1)**.5%1and(n+2)**.5%1)]

Try it online!

Returns the nth term in the sequence. Also using the fact that the nth term = 1 <=> (n+1) or (n+2) is square as Robin Ryder pointed out.

Wolfram Language (Mathematica), 42 bytes

If[#==1||(d=NumberQ@*Sqrt)@#||d[#+1],1,2]&     

returns 1 if n=1 or n is a perfect square or n+1 is a perfect square

Try it online!

JavaScript (ES7),  25 24  23 bytes

Returns the \$n\$-th term, 1-indexed.

n=>n>(++n**.5|0)**2?2:1

Try it online!

How?

We have \$a(n)=2\$ iff the difference between \$n+1\$ and the previous square is greater than \$1\$:

$$n+1-\left\lfloor \sqrt{n+1}\right\rfloor^2>1$$

which boils down to:

$$n>\left\lfloor \sqrt{n+1}\right\rfloor^2$$

R, 27 25 bytes

-1 byte thanks to Giuseppe

1+all((scan()+1:2)^.5%%1)

Try it online!

Uses the fact that \$s_n=1\$ iff \$n+1\$ or \$n+2\$ is a perfect square.

Jelly, 6 bytes

,‘ƲE‘

Try it online!

Explanation

,‘ƲE‘  Main Link:                  N
,       pair with
 ‘                increment         [N, N + 1]
  Ʋ    is it a square?             [issq(N), issq(N + 1)]
    E   are both equal (N and N+1 cannot both be square, so if one is square, it will return 0, and if both are not square, it will return 1)
     ‘  increment (if N or N+1 is square, return 1; otherwise, return 2)
        (implicit input)