# Output a Latin Square

An $$\n\times n\$$ Latin Square is a grid containing exactly $$\n\$$ distinct values where the values in each row and column are distinct. For example,

$$\begin{matrix} A & B & C \\ C & A & B \\ B & C & A \\ \end{matrix}$$

is a Latin square as no row or column contains a repeated value.

You are to take a positive integer $$\n\$$ as input and output an $$\n\times n\$$ Latin Square. The values can be any $$\n\$$ distinct values, and do not have to be consistent for different $$\n\$$. Your program should be consistent and deterministic, so running it with the same input should always produce the same output.

You may output in any reasonable manner, including a flat array consisting of $$\n^2\$$ values, or as a list of $$\n\$$ lists, each containing $$\n\$$ values. You may input and output in any convenient method

This is , so the shortest code in bytes wins.

## Test cases

These are just some possible outputs, your program may differ so long as the output is correct

1 [[1]]
2 [[1, 2], [2, 1]]
3 [[1, 2, 3], [2, 3, 1], [3, 1, 2]]
4 [[1, 2, 3, 4], [2, 1, 4, 3], [3, 4, 1, 2], [4, 3, 2, 1]]
5 [[1, 2, 3, 4, 5], [2, 3, 5, 1, 4], [3, 5, 4, 2, 1], [4, 1, 2, 5, 3], [5, 4, 1, 3, 2]]

• Related. Related. Brownie points for beating or matching my 12 byte Jelly answer Apr 15, 2021 at 20:27
• It there an upper limit on n the code can assume? Apr 16, 2021 at 1:01
• @cnamejj Yeah, you'll never have to handle an integer greater than the square root of the maximum integer in your languages (basically, don't worry about big n so long as your algorithm is sound) Apr 16, 2021 at 1:10

# Python 2, 37 bytes

Outputs a flattened $$\ n \times n \$$ latin square.

lambda n:((range(n)*-~n)[1:]*n)[::~n]


Try it online!

• Very nice! I'll post a bounty when the question becomes eligible.
– xnor
Apr 16, 2021 at 4:58
• @xnor Thanks, I appreciate it! Apr 16, 2021 at 6:17

# Jelly, 3 bytes

RṙⱮ


Try it online!

## Explanation

RṙⱮ  Monadic link, takes an argument z
R    [1, 2, ..., z-1, z]
Ɱ  For each on right argument (z, defaults to loop over range)
ṙ   Rotate the left list by the right amount


Alternative solution: +þ%. Generates the $$\n\times n\$$ addition table and then applies modulo to bring the elements into range.

• First time I've seen caird coinheringaahing get beaten that bad.
– user
Apr 15, 2021 at 20:33
• rip the 12 byte answer Apr 15, 2021 at 20:37
• The advantage of 3 byte answers is that they are easy to bruteforce. :) Apr 17, 2021 at 12:43
• @EricDuminil I wouldn't say 16.7 million is that easy, but I guess it's all subjective, especially since it's not like there's particularly a good way to determine if a 3-byte sequence is a valid solution. Apr 17, 2021 at 19:28
• @hyper-neutrino: Sorry, I didn't want to downplay your impressive achievement. I should have written "possible" instead of "easy". Apr 17, 2021 at 19:37

# APL (dzaima/APL), 4 bytes

⌽ᐵ⍨⍳

⌽ rotate
ᐵ each left, apply each left element to the entire right argument
⍨ apply to both arguments
⍳ range


Try it online!

• Ok, that's really cool. I've not come across ᐵ before. Apr 17, 2021 at 6:18
• @AndrewOgden yeah it's dzaima/APL specific, equivalent in regular dyalog would have to use rank (or ⊂ like bubbler's solution) Apr 17, 2021 at 11:50

# R, 30 bytes

n=scan();outer(1:n,1:n,+)%%n


Try it online!

# APL (Dyalog Unicode), 6 bytes

⍳⌽,⍨⍴⍳


Try it online!

There's already a nice 4-byter in dzaima/APL by rak1507, but I wanted to share a non-trivial short one in vanilla Dyalog. It is shorter than a modulo addition table ⊢|⍳∘.+⍳ and ties with the trivial port of rak's ⍳⌽¨∘⊂⍳.

Takes n and creates a flat matrix of numbers.

⍳⌽,⍨⍴⍳   ⍝ Input: n
,⍨     ⍝ [n, n]
⍴⍳   ⍝ Reshape [1..n] into an n×n matrix
⍝ For n = 4, this looks like this:
⍝ 1 2 3 4
⍝ 1 2 3 4
⍝ 1 2 3 4
⍝ 1 2 3 4
⍳⌽       ⍝ Rotate first row once, second row twice, ..., n-th row n times
⍝ 2 3 4 1
⍝ 3 4 1 2
⍝ 4 1 2 3
⍝ 1 2 3 4


f n=[[x..n]++[1..x-1]|x<-[1..n]]


Try it online!

A kind-of boring approach, just concatenates the ranges.

34 bytes

f n=[mod(k+div(-k)n)n|k<-[1..n*n]]


Try it online!

Outputs a flat list, zero-indexed, using the method from my Python answer. A slight adaptation is needing to make it work for [1..n*n] instead of [0..n*n-1].

# J, 7 bytes

|i.+/i.


Try it online!

Creates an "addition table" of the range 0..n with itself i.+/i. and then takes the mod of each element by n |.

# Julia 1.0, 19 bytes

!n=((N=1:n).+N').%n


Try it online!

# Python 2, 40 bytes

lambda n:[(k+k/n)%n for k in range(n*n)]


Try it online!

Since the challenge allows a flat list of n*n outputs, we make do mapping k=0, 1, ... n*n-1 to the corresponding entry with (k+k/n)%n. We can get this by adding the row index k/n and column k%n and reducing modulo n, and removing the redundant inner %n. We could also do k*-~n/n%n.

Saved 2 bytes thanks to @Delfad0r Would have saved 5 bytes thanks to @AZTECCO (see xnor's answer)

f n=[mod(x+o)n|x<-[1..n],o<-[1..n]]


Try it online!

• -2 bytes if you output a flat list. Apr 15, 2021 at 21:32
• Using mod is a good idea but you can just concat two sequences around 'o' for 32 Apr 15, 2021 at 21:43
• @AZTECCO Thanks! I see xnor posted the same answer (after you suggested your tip), so I think I'll go back to Delfad0r's solution so it isn't the same.
– user
Apr 15, 2021 at 21:47

# Retina, 22 bytes

.+
*
Y\_w
L$.$<'$  Try it online! Explanation: .+ *  Convert the input to unary. Y\_w  Take the first n word characters (or use p for printable ASCII etc.) L$.
$<'$


Create cycled rows. ($<' is the same as $&$'.) # Arturo, 33 bytes $[n][map 0..(n*n)-1'x[(x+x/n)%n]]


Try it on the Arturo Playground! Note: it seems that editing the code edits it for everyone, so don't do that.

# Perl 5-a, 34 bytes

$,=$";say$_.."@F",1..$_-1for 1..$_  Try it online! # Wolfram Language (Mathematica), 23 bytes +##&~Array~{#,#}~Mod~#&  or (as @Roman suggested) Plus~Array~{#,#}~Mod~#&  Try it online! -2 bytes thanks to @att • 23 bytes – att Apr 16, 2021 at 17:18 • Plus~Array~{#,#}~Mod~#& has the same count and is less cryptic. Apr 17, 2021 at 15:37 # Python 2, 43 bytes lambda x,r=range:[r(i,x)+r(i)for i in r(x)]  Try it online! # Japt-m, 3 bytes Output as a 2D array. WéU  Try it Or without the -m flag: ## Japt, 4 bytes ÆZéX  Try it • Ok I can understand the 4bytes one but the 3bytes?OMG! Apr 15, 2021 at 23:33 • @AZTECCO, probably the best kept secret in Japt! Apr 15, 2021 at 23:45 • Realistically both are 4 bytes though, one just moves one byte into the flag. Apr 16, 2021 at 12:23 • @Etheryte, flags don't count towards our scores. Apr 16, 2021 at 16:18 • @Downvoter, what's the problem? Apr 16, 2021 at 16:19 # C (clang), 73 $$\\cdots\$$ 49 46 bytes Saved 3 bytes thanks to dingledooper!!! a;f(n){for(a=n*n;a;)printf("%d ",--a*~n/n%n);}  Try it online! Inputs $$\n\$$ and prints the Latin Square as a flat array consisting of $$\n^2\$$ values. • 46 bytes by iterating backwards and using a shorter formula which outputs negatives instead. Apr 16, 2021 at 0:12 • @dingledooper Nice one with negatives - most ingenious, thanks! :D Apr 16, 2021 at 0:14 # Octave, 21 bytes @(n,x=1:n)mod(x+x',n)  Try it online! The same thing as many other answers... -2 bytes by Giuseppe # JavaScript (ES6), 46 bytes n=>[...Array(n)].map((_,i,a)=>a.map(_=>i++%n))  Try it online! # JavaScript (ES6), 42 bytes As pointed out by @tsh, returning a flatten array is shorter. n=>[...Array(n*n)].map((_,i)=>(i/n+i)%n|0)  Try it online! • use flatten matrix is shorter n=>[...Array(n*n)].map((_,i)=>(i/n+i%n)%n|0) – tsh Apr 16, 2021 at 2:11 # MATL, 3 bytes 5YL  Try it online!  %implicit input n 5YL % create circular matrix of size n  Equivalent to the following Octave/MATLAB code # Octave, 23 bytes @(n)gallery('circul',n)  Try it online! Alternately, this is equivalent to tsh's Octave answer. # MATL, 5 bytes :&+G\  Try it online!  % implicit input n : % range 1:n &+ % add to itself transposed, with broadcast G\ % push n and modulo n % implicit output  # yuno (abandoned), 4 bytes ップア＠％  ## Explanation ップ outer product table; for each (first) and for each (second) ア add ＠ swap top two ％ modulo  Can be written as ppua@%. Code is grouped as [ップ][ア][＠][％]. To prove this is actually 4 bytes, the bytecode is '~\x00ðó' (use the b flag to interpret code from bytecode). # jq, 20 bytes range(.*.)*(1+1/.)%.  Try it online! output is flattened (my first jq answer) # Scala, 44 bytes n=>1 to n flatMap(_=>1 to n)sliding n take n  Try it in Scastie! This is so simple it hardly needs an explanation, but here's one anyway: n => //The input 1 to n //Make a range of n numbers flatMap(_ => 1 to n) //Map each to the range [1, n] and flatten //This creates a list of n*n numbers sliding n //Take n-sized chunks and move ahead by one each time take n //Keep the first n chunks  # Japt, 7 bytes o £o éX  Try it o - [0..U) £ - map by: o éX > rotate X times [0..U)  # PowerShell, 61 bytes param($n)(0..($n*$n-1)|%{[math]::floor($_+$_/$n)%$n})-join" "


Try it online!

# V (vim), 38 30 bytes

"aD@aO0<ESC>V{g<C-A>V}JqqYpdw$pq@a@qdk  Try it online! Input as a single number. Range creating script taken from Leo's Range, Reverse, Sum answer. -8 bytes from Leo using J. Fixed the problem with multidigit numbers. ## Explanation "aD@aO0<ESC>V{g<C-A>V}J "aD delete first line and store in register a @aO0<ESC> print 0 on a newline a times V{g<C-A> convert the zeroes to a range V}J select all, join with spaces$xqqYp2x$pq@a@qdk$x                           remove the last space
qq                         start macro q:
Yp                        duplicate current line
dw                      remove first word (number + space)
\$p                    paste at the end
q                  end macro
@a@q              replay macro a times
dk            delete last two extra iterations

• You can select everything and use J to join lines with spaces more efficiently Try it online!
– Leo
Apr 16, 2021 at 3:33
• ah, yes. I'll add it in. Apr 16, 2021 at 3:35
• Your answer (and my golf) don't work properly for multidigit input though
– Leo
Apr 16, 2021 at 3:37
• lmao, I need to delete a word instead Apr 16, 2021 at 3:38

# Java (JDK), 56 bytes

n->{for(int i=n*n;i-->0;)System.out.println((i/n+i)%n);}


Try it online!

# Python 2, 79 bytes

n=input()
f=lambda b:f(b+b[-1:]+b[-n:-1])if len(b)<n*n else b
print f(range(n))


Try it online!

• Welcome to Code Golf, and nice first answer! Be sure to check out our Tips for golfing in Python page for ways you can golf your program. This doesn't currently output the answer with a print  before the f so you should add that to your score: Try it online!. (cont). Apr 18, 2021 at 19:19
• (cont). Alternatively, you could change it into a version that's just a function (so doesn't need the range when calling, so that you only have to count the function definition Apr 18, 2021 at 19:19
• @cairdcoinheringaahing Edited. I had been testing in the REPL, hence the lack of a print. Apr 18, 2021 at 19:22

# yuno (abandoned), 2 bytes

メリョ


(Disclaimer - rotate was implemented after this challenge, and I'm not sure about map. However, neither of these nor the way empty-stack is treated were designed with this challenge in mind, it just turns out that the way I want my functions to work supports this challenge more nicely than Jelly)

(リョ is one byte)

By abusing the way I manage my stack, we can get this down to two bytes, which is pretty much the minimum possible unless you either have a latin square built-in for some reason, or a "rotate by each in range" built-in.

メ　　  For each element in the second-from-top-of-stack
リョ  Rotate Left


If the stack's size is insufficient, popping will give the first command line argument instead. If that doesn't exist, it will input each time. Thus, you can make this rectangular by using STDIN. Otherwise, supply the number as a command line argument, and it'll use that value for the TOS and second-TOS.

Thus, for each x in 1, ..., N, it rotates N to the left by x, and rotating a number casts it to a range by default.

# 05AB1E, 4 bytes

LD._


Try it online!

LD._  # full program
._  # push...
L     # [1, 2, 3, ...,
# ..., implicit input...
L     # ]...
._  # rotated to the left...
# (implicit) each element in...
LD    # [1, 2, 3, ...,
# ..., implicit input...
LD    # ]
# implicit output