# SuperSquares NxN

Given an integer N from 1-9, you must print an NxN grid of NxN boxes that print alternating 1s and Ns, with each box having an alternating starting integer.

Examples

Input: 1
Output:

1

Input: 2
Output:

12  21
21  12

21  12
12  21

Input: 3
Output:

131  313  131
313  131  313
131  313  131

313  131  313
131  313  131
313  131  313

131  313  131
313  131  313
131  313  131

Input: 4
Output:

1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414

4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141

1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414

4141  1414  4141  1414
1414  4141  1414  4141
4141  1414  4141  1414
1414  4141  1414  4141


An array (of NxN arrays) or text in this format is acceptable.

• Can we output an $N^2 \times N^2$ matrix? Must there be separators between the blocks? Jan 27 at 21:35
• @cairdcoinheringaahing > An array of numbers or text in this format is acceptable. Jan 27 at 21:37
• @Fmbalbuena That doesn't answer the question Jan 27 at 21:40
• @cairdcoinheringaahing Any data type that expresses separated boxes of numbers in this format acceptable Jan 27 at 21:41
• @drmosley So is [[1,2,2,1],[2,1,1,2],[1,2,2,1],[,2,1,1,2]] an acceptable output for $N = 2$? Jan 27 at 21:43

# Wolfram Language (Mathematica), 30 29 bytes

#^Array[Plus,#+0{,,,}]~Mod~2&


Try it online!

Returns a $$\(N\times N)\times(N\times N)\$$ array.

  Array[    ,#+0{,,,}]          N x N x N x N array, where values are
#^                                N^
Plus                        (sum of indices
~Mod~2         mod 2)

• The power trick is a nice touch. Jan 27 at 23:33

# J, 33 23 19 bytes

]^2|i.+/2|+/^:2~@i.


Try it online!

Note: In TIO I added a formatting function to box each element, which is now a list of digits, to make the correctness more obvious. If you remove it, the results will still be correct but because of the way J prints by default it won't be immediately clear.

-10 thanks to power trick stolen from att's Wolfram answer

• 2|...+/^:2~@i. Creates a 0-1 checkerboard in the n x n x n dimensions of the input
• i.+ Creates n versions of that, adding 0, 1, ... n elementwise.
• ]^ Raises input to those 0-1 matrices, creating matrices of 1 and n. This is really the end of the golf.

# Vyxal, 16 bytes

₀Ẏ:v꘍:£vv‡¥꘍?‹*›


Try it Online!

A huge mess.

• This was one of my first thoughts too but I don't think it qualifies. Try it with 4, for example. Jan 27 at 21:59
• @Jonah Oops. (filler) Jan 27 at 22:00
• @Jonah Fixed. (filler) Jan 27 at 22:03
• According to this comment, you may still need some additional separation rather than the single block? Though tbh I am not entirely sure... Jan 27 at 22:08
• Looks correct now. Jan 28 at 3:00

# GeoGebra, 92 bytes

n=2
a=Zip(Zip(Mod(b-a,2),b,1…n),a,1…n)
b=(n-1)a+1
l=Zip(Zip(b+Mod(d,2)(n+1-2b),d,c),c,a)


Try It On GeoGebra!

n is the input, and l is the output.

The output is in the form of an array of depth 4:

• Inside the outermost list (layer 1) is the rows of the supersquare (layer 2)
• Inside the second layer is each individual matrix in the supersquare (layer 3)
• Insde the third layer is each row of a particular matrix entry in the supersquare (layer 4)

The default input is n=2, but you can change this to whatever input you want.

Output for n=2 (layers labelled):

{{{{1, 2}, {2, 1}}, {{2, 1}, {1, 2}}}, {{{2, 1}, {1, 2}}, {{1, 2}, {2, 1}}}}
||||__4_|        |                  |                                      |
|||______3_______|                  |                                      |
||_______________2__________________|                                      |
|____________________________________1_____________________________________|


# Pari/GP, 48 bytes

n->matrix(n,,i,j,matrix(n,,k,l,n^((i+j+k+l)%2)))


Try it online!

# 05AB1E, 10 bytes

L4ãOÉmIFIô


The modulo-2 exponent trick is taken from @att's Mathematica answer, so make sure to upvote him as well!

Explanation:

L           # Push a list in the range [1,(implicit) input]
4ã         # Create all possible quartets of these values,
# by using the cartesian power of 4
O        # Sum each inner list
É       # Check for each sum whether it's odd
m      # Take the (implicit) input to the power of these 0s/1s,
# so all 0s become 1 and all 1s become the input-digit
IF    # Loop the input amount of times:
Iô  #  Split the list into an input amount of parts
# (after which the multi-dimensional matrix is output implicitly)


# C (gcc), 114 111 bytes

k;j;i;f(n){for(i=0;n*n/++i;puts(i%n?"":"\n"))for(j=0;n/++j;putchar(32))for(k=0;n/++k;)printf("%d",i+j+k&1?:n);}


Try it online!

Saved 3 bytes thanks to ceilingcat!!!

# Charcoal, 24 bytes

ＮθＥθＥθ⪫Ｅθ⭆θ∨¬﹪⁺⁺⁺ιλνπ²θ


Try it online! Link is to verbose version of code. Explanation:

Ｎθ                          Input n as an integer
θ                        Input n
Ｅ                         Map over implicit range
θ                      Input n
Ｅ                       Map over implicit range
θ                   Input n
Ｅ                    Map over implicit range
θ                 Input n
⭆                  Map over implicit range and join
⁺⁺⁺ιλνπ       Sum of all indices
¬﹪       ²      Is divisible by 2
∨                Logical Or
θ     Input n
⪫                     Join with spaces
Implicitly print


# Python, 89 bytes

f=lambda n,s=" \n\n",p=0:s.join(j[s>"":]or f(n,s[:-1],j>"1")for j in(n*str(10+n))[p:n+p])

Attempt This Online!

Returns a single string. Most lines have a trailing space.

# Python 3, 181 bytes

def s(n):
a=f'1{n}'*n
b,c=' '.join(([a[:n],a[1:n+1]]*n)[:n]),' '.join(([a[1:n+1],a[:n]]*n)[:n])
return '\n\n'.join((['\n'.join(([b,c]*n)[:n]),'\n'.join(([c,b]*n)[:n])]*n)[:n])

1. Creates a single row for a single box
2. Makes a complete row (N rows) of alternating boxes
3. Return a string containing N rows of N-rows boxes

# Labra, 69 bytes

()[]([])
()[<><{}>]<[{}<[]>]>
<[]><<>>[](<>[])<<>>
{}(())<<><<>><<>>>


I've been waiting for a month and a half for a question Labra could actually compete in.

Prints as a list: [[[[1, 2], [2, 1]], [[2, 1], etc...

For input = 3 (Which Labra receives as [3])
()[]([])                    [1,0]
()[<><{}>]                  Starting at n=1, n=[1,0][n]: [1,0,1,0,1,...]
<[{}<[]>]>        Index with [1...input[1]]:   [0,1,0]
<[]><<>>                    Index into first line:       [1,0,1]
[](<>[])            Put in list and add [0,1,0]: [[1,0,1],[0,1,0]]
<<>>        Index with [0,1,0]:          [[1,0,1],[0,1,0],[1,0,1]]
{}(())                      Input (already a list) + 1:  [3,1]
<          >          Index that with
<>                   previous line:       [[1,0,1],[0,1,0],[1,0,1]]
<<>>               indexed with itself: [[[0,1,0],[1,0,1],[0,1,0]],[[1,0,1],[0,1,0],...
<<>>           twice:               [[[[1,0,1],[0,1,0],[1,0,1]],[[0,1,0],[1,0,1],[0,1,0]],...
giving:                      [[[[1,3,1],[3,1,3],[1,3,1]],[[3,1,3],[1,3,1],[3,1,3]],...
(implicit output)


# PowerShell Core, 111 bytes

param($a)($s=1..($a*$a))|%{$b++ -join($s|%{"1$a"[($_+$b)%2] .($g={if(!($_%$a)){$args;$b+=1-$a%2}})' '}) .$g ''}


Try it online!

-2 bytes thanks to mazzy!
-9 bytes thanks to mazzy again!

• nice. Try it online! ? Jan 28 at 4:52
• Try it online! ¯\_(ツ)_/¯ Jan 29 at 8:37

# JavaScript (Node.js), 56 bytes

f=(n,w=4,y)=>w?[...Array(n)].map(_=>f(n,w-1,y=!y)):y?n:1


Try it online!

• This is completely invalid. Just read the output. Jan 28 at 3:17
• Yes... but the output is infinite... Jan 28 at 3:24
• @Makonede Copied wrong tio
– l4m2
Jan 28 at 3:30

# PHP, 129 bytes

for(;$a++<$n=$argn;){for($b=0;$b++<$n;){for($c=0;$c++<$n;){for($d=0;$d++<$n;)echo($a+$b+$c+$d)%2?\$n:1;echo" ";}echo"
";}echo"
";}


I feel like there's probably a way that this could be done better with a recursive function, but I can't wrap my head around it.

Try it online!