| Bytes | Lang | Time | Link |
|---|---|---|---|
| 025 | Wolfram Language Mathematica | 201015T170812Z | att |
| 010 | Husk | 201015T160219Z | Razetime |
| 024 | Python 2 | 200314T015032Z | xnor |
| 031 | Haskell | 201015T193332Z | xnor |
| 036 | Haskell | 201015T180328Z | lynn |
| 016 | MAWP 0.0 | 200622T051224Z | Dion |
| 021 | cQuents | 200409T033145Z | PkmnQ |
| 017 | dirt verbose mode | 200409T032150Z | cardboar |
| 037 | Python 3 | 200409T062117Z | Dion |
| 075 | C clang | 200316T213043Z | Christia |
| 027 | Perl 6 | 200314T014617Z | Jo King |
| nan | Perl 5 | 200315T153553Z | andytech |
| 055 | PowerShell | 200314T163651Z | Wasif |
| 033 | Symja | 200314T043726Z | lyxal |
| 007 | W | 200314T044352Z | user9206 |
| 016 | GolfScript | 200314T034409Z | user9206 |
| 018 | dc | 200314T010814Z | Mitchell |
| 011 | Charcoal | 200314T010108Z | Neil |
| 056 | C gcc | 200313T160721Z | S.S. Ann |
| 041 | Bash | 200313T164356Z | Noodle9 |
| 050 | C gcc lm | 200313T165842Z | Noodle9 |
| 006 | 05AB1E | 200313T214657Z | Kevin Cr |
| 051 | Haskell | 200313T200219Z | Benji |
| 031 | APL+WIN | 200313T190637Z | Graham |
| 067 | APL Dyalog Unicode | 200313T180942Z | Jeff Zei |
| 031 | Python 3.8 prerelease | 200313T161331Z | Surculos |
| 023 | PowerShell | 200313T181114Z | AdmBorkB |
| 072 | Brainfuck | 200313T180246Z | vityavv |
| 034 | Python 3 | 200313T162904Z | RGS |
| 052 | Python 2 | 200313T162846Z | Alec |
| 042 | Wolfram Language Mathematica | 200313T161320Z | ZaMoC |
| 023 | JavaScript ES7 | 200313T154712Z | Arnauld |
| 025 | R | 200313T155411Z | Robin Ry |
| 006 | Jelly | 200313T155629Z | hyper-ne |
Wolfram Language (Mathematica), 26 25 bytes
Count[√#|√+++#,_@__]&
1-indexed.
√#|√+++# sqrt(n),sqrt(n+1)
Count[ ,_@__] how many aren't perfect squares?
Husk, 14 12 10 bytes
ṁ`Jḋ3MRİ12
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)
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, 36 bytes
do n<-[0..];'1':([1..n]>>"22")++"21"
This expression evaluates to an infinite string.
MAWP 0.0, 16 bytes
[1:![2:1A]%1:2M]
cQuents, 21 bytes
_+(Tr$)%1)=Tr_+$)%1))
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
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
...
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;}
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;}
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*}
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..$_)[$_]
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).
Symja, 38 33 bytes
f(x_):=If(Mod(-(x^.5),1)*x<1,1,2)
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.
dc, 18 bytes
?d2+vd*rvd*-d/2r-p
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.
D
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!
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\$.
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
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);}
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):
∞ü‚Ų€Ë>
Previous 9 bytes variations for the infinite sequence:
Y∞иXδš€û˜: Try it online.ÅÉÅ2Xδ.ø˜: Try it online.
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←⎕]
APL (Dyalog Unicode), 67 bytes
This answer returns the entire sequence up to the requested value.
f←{(⊃,/1,¨⌽¨1,¨2⍴⍨¨1+2×1-⍨⍳1+⌈⍟⍵)[⍳⍵]}
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+⌈⍟⍵)}
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+⌈⍟⍵}
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)
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]
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.
f"{1:2<{x}}1"evaluates to"122..21"withx-1characters"2". (The first"1"is left justified to a block of widthx, with"2"being the fill character).-~i*2is equivalent to(i+1)*2
Thus f"{1:2<{-~i*2}}1" evaluates to the string "122..21" with i*2+1 characters "2".
Brainfuck, 72
+++++++[>+++++++<-]>[>+>+<<-]>>+[<.>>+[>+>+<<-]>[<<.>>-]>[<<+>>-]<<<<.>]
Might be able to make it shorter but here's my shot. Prints indefinetly
- Initialize first two cells with (ASCII values for) 1 and 2
- Start a loop (looping on the 2 cell but it doesn't really matter)
- Print the 1
- Increase the count, and then copy the count to the next two cells
- Use one of the counts to print the twos (decreases the count until 0, then exits loop)
- Copy the second copy of the count back to where it was before
- 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.
Python 2: 52 bytes
def g(n):return(2,1)[not((n+1)**.5%1and(n+2)**.5%1)]
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
JavaScript (ES7), 25 24 23 bytes
Returns the \$n\$-th term, 1-indexed.
n=>n>(++n**.5|0)**2?2:1
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)
Uses the fact that \$s_n=1\$ iff \$n+1\$ or \$n+2\$ is a perfect square.
Jelly, 6 bytes
,‘ƲE‘
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)