g | x | w | all
Bytes Lang Time Link
090Tcl170615T233622Zsergiol
052Janet250325T101547Zxigoi
092SAKO250324T151748ZAcrimori
057AWK250303T214944Zxrs
039Bash + rs170615T183723ZDigital
247Bespoke250216T131627ZJosiah W
053Zsh250216T071725Zroblogic
032Arturo230301T220015Zchunes
006Uiua231113T055927Zchunes
005Thunno 2230622T172822ZThe Thon
029TIBasic230322T181324ZYouserna
089Go230325T100705ZThe Thon
005Vyxal230302T095610ZThe Thon
008Pip S230322T202635ZDLosc
004Japt m230321T121034ZShaggy
005Nekomata230302T001036Zalephalp
007Pyt230302T125020ZKip the
nan230302T094648ZThe Thon
005MATL170615T213405ZLuis Men
006Husk201022T121810ZLegionMa
059C gcc170908T202832Zceilingc
043Haskell170908T032502ZWheat Wi
049Perl 5170908T024229ZXcali
063C170615T195719ZSteadybo
142SwiProlog170620T083033ZTessella
016q/kdb+170616T173653Zmkst
066Python170616T071653ZDaffy
041J170617T013708ZTessella
021braingasm170616T213504Zdaniero
088C# .NET Core170616T202936ZCharlie
081Common Lisp170616T190525Zuser6516
077Java OpenJDK 8170615T192806ZPunPun10
037R170616T110213Zuser2390
00705AB1E170616T062952Zkalsower
082Bash & coreutils170616T094135ZWossname
028Mathematica170616T075807ZGreg Mar
030Retina170615T184538ZNeil
145C#170616T072122ZErlantz
024Octave170616T051148Zalephalp
023Mathematica170616T052104Zalephalp
009J170616T002106Zmiles
087///170615T230534Zboboquac
5146JavaScript ES6170615T183659ZDowngoat
036Clojure170615T211442ZNikoNyrh
038Haskell170615T175912ZJulian W
009Pyth170615T201508ZKarlKast
015Brachylog170615T195211ZFatalize
054Python 2170615T183638Ztotallyh
043Python170615T192408Zxnor
015Actually170615T192217ZLeaky Nu
038Cheddar170615T191652ZLeaky Nu
008Charcoal170615T183855ZNeil
038Julia170615T182138ZJulian W
017CJam170615T181550ZErik the
015V170615T174325ZDJMcMayh
056PHP170615T175138ZJör
019Brachylog170615T180751ZLeaky Nu
019QBIC170615T175923Zsteenber
007MATL170615T174740ZStewie G
025Mathematica170615T175534ZMartin E
028Mathematica170615T174439ZZaMoC
008APL Dyalog170615T174251Zuser4180
006Japt170615T173657ZETHprodu
004Jelly170615T173308ZLeaky Nu

Tcl, 90 bytes

proc C n {time {time {puts -nonewline [expr [incr i]%2]} $n
puts ""
if $n%2<1 incr\ i} $n}

Try it online!

Janet, 52 bytes

|(seq[i :down[$ 0]](seq[j :down[$ 0]](%(+ i j 1)2)))

SAKO, 92 bytes

PODPROGRAM:F(N)
CALKOWITE:N,I
*)DRUKUJ(0):MOD(I,2)
SPACJI2×0*MOD(I,N)
POWTORZ:I=1(1)N*2
WROC

Outputs a list of lists.

Full programme version, 94 bytes

CALKOWITE:N,I
CZYTAJ:N
*1)DRUKUJ(0):MOD(I,2)
SPACJI2×0*MOD(I,N)
POWTORZ:I=1(1)N*2
STOP1
KONIEC

AWK, 57 bytes

{for(;i++<$1^2;$1%2||i%$1||x=!x)printf(x=!x)(i%$1?FS:RS)}

Attempt This Online!

Bash + rs, 39

eval echo \$[~{1..$1}+{1..$1}\&1]|rs $1

Try it online!

Bespoke, 247 bytes

daily I am torn
swaying between both of my given options
jumping to zero,then one,as usual
skipping over each in repeated cycles
anything I decide,I suddenly override
in this limited box,put in a restricted matrix
so am I evermore confined in here

For each X and Y value from n down to 1, I print !((x + y) % 2).

Zsh, 53 bytes

(for i ({1..$1})for j ({1..$1})<<<$[(i+j+1)%2])|rs $1

Try it online!

Arturo, 39 36 32 bytes

$[n][map n'x->map n'y->%x+y+1 2]

Try it

-3 thanks to alephalpha's simpler method

Prettified and rearranged for clarity:

$[n][                     ; a function taking an argument n
    map n 'x ->           ; map over 1..n, assign current elt to x
        map n 'y ->       ; map over 1..n again, assign current elt to y
            (x+y+1)%2     ; what it looks like
]                         ; end function

Uiua, 6 bytes

⊞=.◿2⇡

Try it!

⊞=.◿2⇡
     ⇡  # range
   ◿2   # modulo 2
  .     # duplicate
⊞=      # table by equality

Thunno 2, 5 bytes

RɗDȷ=

Attempt This Online!

Port of my Vyxal answer.

Explanation

       # implicit input
R      # one range
 ɗ     # mod 2
  D    # duplicate
   ȷ   # outer product:
    =  #  equality
       # implicit output

TI-Basic, 30 29 bytes

identity(Ans→[A]
Fill(1,[A]
cumSum([A]
2fPart(.5(Ans+Ansᵀ+[A]

Takes input in Ans.
-1 byte thanks to MarcMush

Go, 89 bytes

import."fmt"
func f(n int){
for i:=1;i<=n;i++{
for j:=i;j<i+n;j++{Print(j%2)}
Println()}}

Attempt This Online!

Prints a multi-line string.

Explanation

import ."fmt"               // Boilerplate
func f(n int) {             // Function f taking an integer n:
for i := 1; i <= n; i++ {   //  Loop through each i in [1..n]:
for j := i; j < i+n; j++ {  //   Loop through each j in [i..i+n]:
Print(j%2)}                 //    Print j mod 2 without a newline
Println()}}                 //   Print a newline

Vyxal, 5 bytes

ʁ∷:v=

Try it Online!

-3 thanks to @lyxal
-1 thanks to @emanresuA

Explanation

ʁ∷:v=    implicit input
ʁ        range(input)
 ∷       mod 2
  :      duplicate
   v=    equals, vectorised over the top list
         (generates a nested list)

Old:

ɾ∷ẋ⁽†ẇ    implicit input
ɾ         range(1, input+1)
 ∷        mod 2
  ẋ       repeated input times
     ẇ    apply to every 2nd item:
   ⁽†     vectorised not
ʁ:∷:†$"$İ    implicit input
ʁ:           range, duplicate
  ∷:         mod 2, duplicate
    †        vectorised not
     $"      swap and pair
       $İ    swap and index in

Pip -S, 8 bytes

%U$+GMCa

Other list-formatting flags such as -l and -p work too. Without a flag, the list of lists is printed without any separators (e.g. 1001 for input 2), which seemed like stretching the permissive output format a bit too far.

Attempt This Online!

Explanation

       a  ; Command-line argument
     MC   ; Map to a square coordinate grid of that size:
  $+G     ;   Sum the two coordinates
 U        ;   Increment
%         ;   Mod 2

Japt -m, 5 4 bytes

Outputs a 2D array.

W£°v

Try it

Nekomata, 5 bytes

ᵒ+→2%

Attempt This Online!

ᵒ+→2%
ᵒ+       Create an (input x input) matrix where the element at (i,j) is i+j
  →      Increment
   2%    Mod 2

Pyt, 7 bytes

ř2%Đɐ=Ɩ

Try it online!

ř                    implicit input; řangify
 2%                  modulo 2
   Đ                 Đuplicate
    ɐ=               for ɐll possible pairs, check for equality
      Ɩ              coerce booleans to integers; implicit print

Prints list of lists

Thunno, \$ 12 \log_{256}(96) \approx \$ 9.88 bytes

RD2%D1_ZPsAH

Attempt This Online! or see it in a nicer format

Explanation

RD2%D1_ZPsAH  # Implicit input                Example: n=3
RD            # Duplicate range(input)        STACK: [0, 1, 2],  [0, 1, 2]
  2%D         # Duplicate ^ mod 2             STACK: [0, 1, 2],  [0, 1, 0],  [0, 1, 0]
     1_       # 1 - top copy of ^             STACK: [0, 1, 2],  [0, 1, 0],  [1, 0, 1]
       ZP     # Pair top two items            STACK: [0, 1, 2],  [[0, 1, 0], [1, 0, 1]]
         sAH  # 1-based modular indexing      STACK: [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
              # Implicit output

MATL, 5 bytes

:otYT

Try it at MATL online!

Explanation

Consider input 4 as an example.

:    % Implicit input, n. Push range [1 2 ... n]
     %   STACK: [1 2 3 4]
o    % Parity, element-wise
     %   STACK: [1 0 1 0]
t    % Duplicate
     %   STACK: [1 0 1 0], [1 0 1 0]
YT   % Toeplitz matrix with two inputs. Implicit display
     %   STACK: [1 0 1 0;
     %           0 1 0 1;
     %           1 0 1 0;
     5           0 1 0 1]

Husk, 6 bytes

´Ṫ=↑ݬ

Try it online!

C (gcc), 60 59 bytes

i;f(n){for(i=n*n;i--;)printf(i%n?"%d":"%d\n",i%n+i/n+1&1);}

Try it online!

Haskell, 43 bytes

h=1:0:h
l=h:(0:h):l
s x=take x$map(take x)l

Try it online!

This creates the infinite checkerboard matrix and saves it to l. Then our function s chops off a square.

Perl 5, 49 bytes

$_=10x(($n=<>)/2).1x($n%2);say&&y/01/10/while$n--

Try it online!

C, 69 67 63 bytes

Thanks to @Kevin Cruijssen for saving two bytes and @ceilingcat for saving four bytes!

i,j;f(n){for(i=n;i--;puts(""))for(j=n;j;)printf("%d",j--+i&1);}

Try it online!

Swi-Prolog, 142 bytes.

t(0,1).
t(1,0).
r([],_).
r([H|T],H):-t(H,I),r(T,I).
f([],_,_).
f([H|T],N,B):-length(H,N),r(H,B),t(B,D),f(T,N,D).
c(N,C):-length(C,N),f(C,N,1).

Try online - http://swish.swi-prolog.org/p/BuabBPrw.pl

It outputs a nested list, so the rules say:

Test Cases: enter image description here

q/kdb+, 37 16 bytes

Solution:

{x#x#'(1 0;0 1)}

Example:

q){x#x#'(1 0;0 1)}1
1
q){x#x#'(1 0;0 1)}2
1 0
0 1
q){x#x#'(1 0;0 1)}3
1 0 1
0 1 0
1 0 1
q){x#x#'(1 0;0 1)}4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

Explanation:

2nd version is much simpler, and thus shorter and faster (4x). Create a 2-item list containing 01... and 10... to the length of the input, then take 'x' number of items from this new list.

{              } / lambda function
      (1 0;0 1)  / 2-item list of (0;1) and (1;0)
   x#'           / take 'x' items from each list, if x=3 then (1 0 1;0 1 0)
 x#              / take 'x' items from *this* list

Notes:

I've re-written this twice during this edit, went from 37->25->24->16 bytes. Now it's a little more competitive.

Edits:

Python, 73 63 62 66 bytes

Saved 4 bytes thanks to officialaimm

r=range(input());print[''.join([`(x+y+1)%2`for x in r])for y in r]

J, 41 bytes

I know there's a J answer at 9, but I'm pleased to get anything working, even if it's not a tacit programming best possible..

ch=:monad define
2|(2$y)$(1+i.y),(i.y)
)

It takes a number y and generates two lists of numbers 1,2,3,..y and 0,1,2,3,..y-1 to make the offset first and second row, appends them into one long list, reshapes that into a y,y matrix (wrapping around when it runs out), and then does modulo 2 on all the elements to make them a 1 or 0.

   ch 1
1

   ch 2
1 0
0 1

   ch 3
1 0 1
0 1 0
1 0 1

   ch 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

   ch 5
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

braingasm, 21 bytes

;$[$[>+o:<]p[>+<]10.]

Explanation:

;                       Read a number from stdin.
 $[                 ]   That many times...
   $[     ]               That many times...
     >+o:<                  Increase the next cell and print its value modulo 2.
           p[   ]       If the input was even...
             >+<          Increase the next cell once more.
                 10.    Print a newline.

C# (.NET Core), 88 bytes

n=>{var r=new int[n,n];for(int i=0,j;i<n;i++)for(j=0;j<n;j++)r[i,j]=(i+j+1)%2;return r;}

Try it online!

I can't believe this is the shortest way to initialize a 2D array of integers, there must be another way...

Common Lisp, SBCL, 81 bytes

version 1:

(#1=dotimes(i(set'a(read)))(#1#(j a)(format t"~:[1~;0~] "(oddp(+ j i))))(terpri))

version 2:

(lambda(n)(#1=dotimes(i n)(#1#(j n)(format t"~:[1~;0~] "(oddp(+ j i))))(terpri)))

Two loops, and inside I print either 1 and space or zero and space based on parity of (+ j i). (terpri) for newline.

It's long, I know:/

Java (OpenJDK 8), 80 77 bytes

-3 bytes thanks to Kevin Cruijssen

j->{String s="1";for(int i=1;i<j*j;s+=i++/j+i%j&1)s+=1>i%j?"\n":"";return s;}

Try it online!

Oh look, a semi-reasonable length java answer, with lots of fun operators.

lambda which takes an int and returns a String. Works by using the row number and column number using / and % to determine which value it should be, mod 2;

Ungolfed:

j->{
    String s="1";
    for(int i=1; i<j*j; s+= i++/j + i%j&1 )
        s+= 1>i%j ? "\n" : "";
    return s;
}

R, 38 37 bytes

n=scan();(matrix(1:n,n,n,T)+1:n-1)%%2

Try it online!

-1 byte thanks to Giuseppe

Takes advantage of R's recycling rules, firstly when creating the matrix, and secondly when adding 0:(n-1) to that matrix.

05AB1E, 9 7 bytes

-2 bytes thanks to Emigna

LDÈD_‚è

Try it online!

Explanation

LDÈD_‚sè» Argument n
LD        Push list [1 .. n], duplicate
  ÈD      Map is_uneven, duplicate
    _     Negate boolean (0 -> 1, 1 -> 0)
     ‚    List of top two elements of stack
      è   For each i in [1 .. n], get element at i in above created list
          In 05AB1E the element at index 2 in [0, 1] is 0 again

Bash & coreutils, 82 bytes

x=$1;yes 10|tr -d \\n|dd bs=1 count=$[x*x*2]|fold -w$[x*2-1]|grep -om$x "^.\{$x\}"

Takes one command-line integer as input. Apparently the spaces between are optional.

Mathematica, 28 bytes

Cos[+##/2Pi]^2&~Array~{#,#}&

Pure function taking a positive integer as input and returning a 2D array. Uses the periodic function cos²(πx/2) to generate the 1s and 0s.

For a little more fun, how about the 32-byte solution

Sign@Zeta[1-+##]^2&~Array~{#,#}&

which uses the locations of the trivial zeros of the Riemann zeta function.

Retina, 33 30 bytes

.+
$*
1
$_¶
11
10
T`10`01`¶.+¶

Try it online! Explanation: The first stage converts the input to unary using 1s (conveniently!) while the second stage turns the value into a square. The third stage inverts alternate bits on each row while the last stage inverts bits on alternate rows. Edit: Saved 3 bytes thanks to @MartinEnder.

C#, 145 Bytes

int u=1;int n=int.Parse(b.Text);for(int i=0;i<n;i++){if(i%2==0){u=1;}else{u=0;}for(int j=0;j<n;j++){t.Text+=u.ToString();u=1-u;}t.Text+="\r\n";}[enter link description here][1]

Try it online!

Octave, 24 bytes

@(n)~mod((a=(1:n))+a',2)

Try it online!


Or the same length:

@(n)mod(toeplitz(1:n),2)

Try it online!

Mathematica, 23 bytes

ToeplitzMatrix@#~Mod~2&

J, 9 bytes

<0&=$&1 0

Try it online!

///, 87 bytes + input

/V/\\\///D/VV//*/k#D#k/k#D&k/k&DVk/k\D/SD/#/r
DSkk/10DSk/1D&/V#rV#0r;VV0;VVV1;V\D/r/S/&[unary input in asterisks]

Try it online! (input for 4)

Unary input in 1s, 95 bytes + input

/V/\\\///D/VV//&1/k#&D&|D/#k/k#D&k/k&DVk/k\D/SD/#/r
DSkk/10DSk/1D&/V#rV#0r;VV0;VVV1;V\D/r/S/&&[unary input in ones]|

Try it online! (input for 8)

How does this work?

JavaScript ES6, 55 54 51 46 bytes

Saved 1 byte thanks to @Neil

Saved 2 bytes thanks to @Arnauld

n=>[...Array(n)].map((_,i,a)=>a.map(_=>++i&1))

Try it online!

This outputs as an array of arrays. JavaScript ranges are pretty unweildy but I use [...Array(n)] which generates an array of size n

Clojure, 36 bytes

#(take %(partition % 1(cycle[1 0])))

Yay, right tool for the job.

Haskell, 50 41 39 38 bytes

Thanks to nimi and xnor for helping to shave off a total of 9 10 bytes

f n=r[r"10",r"01"]where r=take n.cycle

Alternately, for one byte more:

(!)=(.cycle).take
f n=n![n!"10",n!"01"]

or:

r=flip take.cycle
f n=r[r"10"n,r"01"n]n

Probably suboptimal, but a clean, straightforward approach.

Pyth, 9 bytes

VQm%+hdN2

Try this!

another 9 byte solution:

mm%+hdk2Q

Try it!

Brachylog, 15 bytes

^₂⟦₁%₂ᵐ;?ḍ₎pᵐ.\

Try it online!

Explanation

Example Input: 4

^₂               Square:                            16
  ⟦₁             1-indexed Range:                   [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
    %₂ᵐ          Map Mod 2:                         [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
       ;?ḍ₎      Input-Chotomize:                   [[1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]]
           pᵐ.   Map permute such that..
             .\  ..the output is its own transpose: [[1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]]

Python 2, 72 70 61 54 bytes

-7 bytes thanks to Leaky Nun. -1 byte thanks to Wheat Wizard.

lambda n:[[i-~j&1for i in range(n)]for j in range(n)]

Try it online!

Python, 43 bytes

lambda n:[('10'*n)[i:i+n]for i in range(n)]

Try it online!

Anonymous function that outputs a list like ['1010', '0101', '1010', '0101'].

Actually, 15 bytes

r;╗⌠╜+u⌠2@%⌡M⌡M

Try it online!

Cheddar, 38 bytes

n->(|>n).map(i->(|>n).map(j->i+j+1&1))

Try it online!

Charcoal, 8 bytes

UON10¶01

Try it online! Explanation: This roughly translates to the following verbose code (unfortunately the deverbosifier is currently appending an unnecessary separator):

Oblong(InputNumber(), "10\n01");

Julia, 38 bytes

n->map(x->x%2,(1:n).+transpose(0:n-1))

The usual approach

CJam, 17 bytes

{_[__AAb*<_:!]*<}

Try it online!

Returns a list (TIO link has formatted output).

V, 16, 15 bytes

Ài10À­ñÙxñÎÀlD

Try it online!

Hexdump:

00000000: c069 3130 1bc0 adf1 d978 f1ce c06c 44    .i10.....x...lD

PHP, 56 bytes

Output as string

for(;$i<$argn**2;)echo++$i%2^$n&1,$i%$argn?"":"
".!++$n;

Try it online!

PHP, 66 bytes

Output as 2 D array

for(;$i<$argn**2;$i%$argn?:++$n)$r[+$n][]=++$i%2^$n&1;print_r($r);

Try it online!

Brachylog, 19 bytes

⟦₁Rg;Rz{z{++₁%₂}ᵐ}ᵐ

Try it online!

QBIC, 19 bytes

[:|?[b|?(a+c+1)%2';

Explanation

[:|         FOR a = 1 to b (b is read from cmd line)
?           PRINT - linsert a linebreak in the output
[b|         FOR c = 1 to b
?(a+c+1)%2  PRINT a=c=1 modulo 2 (giving us the 1's and 0's
';            PRINT is followed b a literal semi-colon, suppressing newlines and 
              tabs. Printing numbers in QBasic adds one space automatically.

MATL, 7 bytes

:t!+2\~

Try it online!

Explanation:

         % Implicit input (n)
:        % Range from 1-n, [1,2,3]
 t       % Duplicate, [1,2,3], [1,2,3]
  !      % Transpose, [1,2,3], [1;2;3]
   +     % Add        [2,3,4; 3,4,5; 4,5,6]
    2    % Push 2     [2,3,4; 3,4,5; 4,5,6], 2
     \   % Modulus    [0,1,0; 1,0,1; 0,1,0]
      ~  % Negate     [1,0,1; 0,1,0; 1,0,1]

Note: I started solving this in MATL after I posted the challenge.

Mathematica, 25 bytes

1-Plus~Array~{#,#}~Mod~2&

Mathematica, 28 bytes

Table[Mod[i+j+1,2],{i,#},{j,#}]&

APL (Dyalog), 8 bytes

~2|⍳∘.+⍳

Try it online!

Explanation

Let's call the argument n.

⍳∘.+⍳

This creates a matrix

1+1 1+2 1+2 .. 1+n
2+1 2+2 2+3 .. 2+n
...
n+1 n+2 n+3 .. n+n

Then 2| takes modulo 2 of the matrix (it vectorises) after which ~ takes the NOT of the result.

Japt, 6 bytes

ÆÇ+X v

Test it online! (Uses -Q flag for easier visualisation)

Explanation

 Æ   Ç   +X v
UoX{UoZ{Z+X v}}  // Ungolfed
                 // Implicit: U = input number
UoX{          }  // Create the range [0...U), and map each item X to
    UoZ{     }   //   create the range [0...U), and map each item Z to
        Z+X      //     Z + X
            v    //     is divisible by 2.
                 // Implicit: output result of last expression

An interesting thing to note is that v is not a "divisible by 2" built-in. Instead, it's a "divisible by X" built-in. However, unlike most golfing languages, Japt's functions do not have fixed arity (they can accept any number of right-arguments). When given 0 right-arguments, v assumes you wanted 2, and so acts exactly like it was given 2 instead of nothing.

Jelly, 4 bytes

52 seconds!

+€ḶḂ

Try it online!