| Bytes | Lang | Time | Link |
|---|---|---|---|
| 049 | Clojure | 240906T105223Z | NikoNyrh |
| 184 | Oracle SQL 11.2 | 240905T151026Z | Jeto |
| 045 | F# .NET Core | 240905T050135Z | dtanku |
| 008 | Pyth | 240821T160302Z | int 21h |
| 033 | Arturo | 230117T011748Z | chunes |
| 004 | Husk | 210611T092216Z | Dominic |
| 037 | Perl 5 + M5.10.0 na | 231120T092503Z | Dom Hast |
| 052 | JavaScript Node.js | 231115T021507Z | l4m2 |
| 016 | RProgN 2 | 231115T015831Z | ATaco |
| 002 | Vyxal | 231114T235605Z | lyxal |
| 039 | Perl 5 | 231114T234331Z | Xcali |
| 006 | Uiua | 231114T212505Z | chunes |
| 075 | JavaScript Node.js | 230718T093752Z | Fhuvi |
| 004 | Thunno 2 | 230714T142931Z | The Thon |
| 038 | Excel Office 365 Insider Preview | 220313T015604Z | Taylor R |
| 044 | Excel VBA | 220309T203434Z | Engineer |
| 012 | Pyth | 220309T181739Z | sinvec |
| 026 | Pari/GP | 211201T012836Z | alephalp |
| 122 | Nim | 210930T235753Z | Qaziquza |
| 005 | ayr | 211201T144106Z | ZippyMag |
| 004 | Vyxal 2.6.0 r | 211201T110337Z | lyxal |
| 084 | Rust | 211201T084408Z | Ezhik |
| 041 | TIBasic | 210930T224219Z | Youserna |
| 052 | Python | 210915T140326Z | Larry Ba |
| 080 | Regenerate | 210628T201632Z | DLosc |
| 277 | TSQL | 210628T135259Z | LiefdeWe |
| 049 | Python 3 | 210613T205143Z | Kyuuhach |
| 9389 | <>^v | 210611T150735Z | astroide |
| 067 | Python 3 | 210612T065432Z | user1045 |
| 016 | Wolfram Language Mathematica | 210611T214326Z | Greg Mar |
| 028 | Haskell | 210611T211222Z | xnor |
| 006 | 05AB1E | 210611T164155Z | Makonede |
| 069 | PHP F | 210611T143748Z | Kaddath |
| 048 | Python 2 | 210611T115700Z | ElPedro |
| 056 | JavaScript Node.js | 210610T183228Z | user1007 |
| 070 | C# Visual C# Interactive Compiler | 210611T093914Z | LiefdeWe |
| 084 | Java JDK | 210611T082509Z | Olivier |
| 005 | Japt | 210610T170213Z | Shaggy |
| 035 | Ruby | 210611T050243Z | G B |
| 006 | Vyxal | 210611T042759Z | emanresu |
| 049 | Python 3 | 210611T035134Z | att |
| 040 | PowerShell Core | 210611T032831Z | Julian |
| 035 | Factor | 210611T025535Z | chunes |
| 003 | Stax | 210611T025507Z | Razetime |
| 019 | Octave | 210611T024808Z | tsh |
| 030 | R | 210610T202338Z | pajonk |
| 065 | C gcc | 210610T194119Z | dingledo |
| 011 | Charcoal | 210610T192633Z | Neil |
| 077 | C clang | 210610T182302Z | Noodle9 |
| 010 | K ngn/k | 210610T190457Z | coltim |
| 019 | Julia | 210610T184516Z | MarcMush |
| 096 | Posix SH + sed | 210610T175639Z | Wezl |
| 015 | Vyxal | 210610T174043Z | Wasif |
| 085 | JavaScript Node.js | 210610T173403Z | user1006 |
| 050 | Python 2 | 210610T165642Z | hyper-ne |
| 009 | J | 210610T171717Z | Conor O& |
| 011 | APL Dyalog Unicode | 210610T170242Z | Wasif |
| 005 | APL Dyalog Unicode | 210610T164902Z | Adá |
| 002 | Jelly | 210610T164853Z | caird co |
Clojure, 49 bytes
#(for[R[(range 1(inc %))]r R](for[c R](max r c)))
TIO. IMO the only interesting trick is to re-use the range value R.
Oracle SQL 11.2, 184 bytes
WITH v AS(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=:1)SELECT DISTINCT LISTAGG(DECODE(SIGN(a.l-b.l),1,a.l,b.l))WITHIN GROUP(ORDER BY a.l)OVER(PARTITION BY b.l)FROM v a,v b ORDER BY 1;
Un-golfed
WITH v AS(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=:1)
SELECT DISTINCT LISTAGG(DECODE(SIGN(a.l-b.l),1,a.l,b.l))WITHIN GROUP(ORDER BY a.l)OVER(PARTITION BY b.l)
FROM v a, v b
ORDER BY 1;
F# (.NET Core), 45 bytes
let a n=Array2D.init n n (fun i j->max i j+1)
F#'s built-in array initializers come in clutch here (cf. the C# answers).
Pyth, 8 bytes
VQmhg#dN
Basically an improvement of this 12 byte answer (please upvote it). To find the maximum value with g# I have used this tip from @isaacg.
Commented:
VQ # for N in 0..Q-1
m Q # map over 0..Q-1 (implicit):
g#dN # maximum of mapping variable d and N
h # then add 1
Husk, 4 bytes
´ṪYḣ
Outputs as a 2d matrix.
´ # argdup: apply a function to two copies of the same argument
Ṫ # Ṫable: make a 'multiplication table' ...
Y # ... using function Y = maximum of two values
ḣ # with (duplicated) argument ḣ = range from 1 to input number
7 bytes - ¶mw´ṪYḣ - to ouput in the exact same format as the examples in the question.
JavaScript (Node.js), 52 bytes
n=>(g=(i,j)=>i?[...g(i-1,j),j?i<j?j:i:g(n,i)]:[])(n)
JavaScript (Node.js), 55 bytes
n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>1+(x+=y>x)))
From user100752(deleted)'s answer
RProgN 2, 16 bytes
x=x«y=x²yMS•.p»;
Explaination
x=x«y=x²yMS•.p»;
x= # Set x to the input
x« »; # For 1 - x
y= # Assign y to the iterator
x² S # Generate a stack from a pair-function
yM # The max of y and the current index
•.p # Join via spaces, and print
JavaScript (Node.js), 75 bytes
Not the best JS solution for a string output, but really close!
I tried to gain bytes by parsing only a single one-dimensional array, but lost some bytes in calculations, and i don't find how to shorten it further.
n=>[...Array(n*n)].map((_,i)=>Math.max(i%n,i/n|0)+1+(++i%n?' ':`
`)).join``
Thunno 2, 4 bytes
RDȷ§
Explanation
RDȷ§ # Implicit input
R # Push [1..input]
D # Duplicate
ȷ # Outer product over:
§ # Dyadic maximum
# Implicit output
Excel (Office 365 Insider Preview), 38 bytes
An anonymous worksheet function that takes input \$n\$ from cell A1 and outputs an \$n\times n\$ dynamic array to the calling cell.
This solution makes use of the =MAKEARRAY() and =LAMBDA() functions, which are currently only available via the Office Insiders program.
=MakeArray(A1,A1,Lambda(r,c,Max(r,c)))
Annotated
=MakeArray(A1,A1, ) ' Make an nxn array holding row & col index
Lambda(r,c, ) ' Pass the indices into a function
Max(r,c) ' Take the larger of two at each position
' Output as nxn dynamic array
Excel VBA, 45 44 bytes
Saved 1 bytes thanks to Taylor Raine because of course. At least now it's my favorite number of bytes.
[A1].Resize([A1],[A1])="=Max(Row(),Column())"
Input is in the cell A1 of the active sheet. Program is run from the immediate window. It works by writing a formula into a range of cells. This will also overwrite the input cell with a formula.
Nim, 138 122 bytes
proc x(i:int)=
for x in 1..i:
for j in 1..x:stdout.write($x&" ")
for j in x+1..i:stdout.write($j&" ")
echo()
ayr, 5 bytes
^:\`~
Explained
~ One-range to N
` Commute (copy arg to make next symbol dyadic)
\ Table
^: Of maxes
Vyxal 2.6.0 r, 4 bytes
ẊvGẇ
Feels good to have Cartesian product use ranges for numbers now. Posting as a separate answer to emanresuA because it uses features not available in the version they used (haha rewrite go brrr).
Explained
ẊvGẇ
Ẋ # The Cartesian Product of the ranges [1, input] and [1, input]
vG # the maximum of each pair
ẇ # wrapped to chunks of length input
Rust, 84 bytes
|n|(1..=n).fold("".into(),|a,i|(1..=n).fold(a,|b,j|format!("{}{} ",b,i.max(j)))+"
")
The output will be a String. The newline is given literally inside quotes, which saves one byte.
TI-Basic, 38 41 bytes
Input N
identity(N→[A]
For(I,1,N
For(J,1,N
max(I,J→[A](I,J
End
End
[A]
Output is stored as a matrix in Ans and is displayed.
Regenerate, 80 bytes
((${$2+1}|1)( $2){$2-1}( ${$4+1}{1-$4/$~1}| ${$2+1}{1-$2/$~1}){$~1-$2+1}\n){$~1}
Takes the argument from the command-line. Try it here!
Explanation
(Spaces are replaced with underscores to improve visibility.)
Each line is generated in three parts. Top-level structure:
( ) Group 1: a line of output
(...) Group 2: the first digit
(...){...} Group 3: repetitions of the first digit
(...){...} Group 4: count up to N
\n Add a newline
{$~1} Repeat, generating N lines
Group 2, the first digit on each line:
( )
${$2+1} Add 1 to the previous value of group 2
| Or, if group 2 has not been previously matched...
1 Use 1
Group 3, the rest of the digits on the line that are identical to the first number:
(_$2) The most recent value of group 2 (with a space in front of it)
{$2-1} repeated (group 2) - 1 times
Group 4, the remaining numbers on the line, starting with (group 2) + 1 and counting up to N:
( ${$4+1}{1-$4/$~1}| ${$2+1}{1-$2/$~1}){$~1-$2+1}
( )
_ Space
${$4+1} Previous value of group 4, plus 1
{ } Repeat the value this many times:
$4/$~1 1 if group 4 == N, 0 otherwise
1- Subtract from 1: 1 -> 0 and 0 -> 1
So if the new value would have been N+1,
it instead becomes empty string
| If the previous value of group 4 was not
a number, the arithmetic fails, and we
instead use...
_ Space
${$2+1} Last value of group 2, plus 1
{1-$2/$~1} Repeat 0 times if group 2 == N
{ } Repeat the above
$~1-$2+1 N - (group 2) + 1 times
Worked example
Suppose the input is 3.
Line 1:
- Group 2: not previously matched, so
1 - Group 3:
1repeated1-1= 0 times, so empty string - Group 4 is repeated
3-1+1= 3 times:- Group 4 not previously matched, so
followed by2repeated1-1/3= 1 time (the division is integer division) - Previous value of group 4 is
2, sofollowed by3repeated1-2/3= 1 time - Previous value of group 4 is
3, sofollowed by4repeated1-3/3= 0 times
- Group 4 not previously matched, so
Result: 1 2 3
Line 2:
- Group 2: previous value is
1, so2 - Group 3:
2repeated2-1= 1 time - Group 4 is repeated
3-2+1= 2 times:- Previous value of group 4 is
, which is not a number, sofollowed by3repeated1-2/3= 1 time - Previous value of group 4 is
3, sofollowed by4repeated1-3/3= 0 times
- Previous value of group 4 is
Result: 2 2 3
Line 3:
- Group 2: previous value is
2, so3 - Group 3:
3repeated3-1= 2 times - Group 4 is repeated
3-3+1= 1 time:- Previous value of group 4 is
, which is not a number, sofollowed by4repeated1-3/3= 0 times
- Previous value of group 4 is
Result: 3 3 3
T-SQL (277 bytes)
Not sure how inputs are handled in SQL but my first declared parameter is the input. I also can't get an online T-SQL compiler that runs this. But this will run in your SSMS just fine.
DECLARE @a int = 7DECLARE @ int=1DECLARE @c int=1DECLARE @b nvarchar(max)='SELECT'WHILE @<=@a BEGIN
SET @c=1WHILE @c<=@a BEGIN
SET @b+=IIF(@c=1,'',',')+STR(IIF(@c<@,@,@c))+'AS['+STR(@c)+']'SET @c+=1
END
SET @b+=IIF(@<@a,'UNION ALL SELECT','')SET @+=1
END
EXEC sp_executesql @b;
It's a naive approach with nothing special, except that it dynamically builds up a query.
The query it generates is:
SELECT 1AS[ 1], 2AS[ 2], 3AS[ 3]UNION ALL SELECT 2AS[ 1], 2AS[ 2], 3AS[ 3]UNION ALL SELECT 3AS[ 1], 3AS[ 2], 3AS[ 3]
<>^v, 93 89 bytes
i,tTv
v`j1<
>I)iIT‹!0jv
v <
>J)jJI≥v_~ v
>~ v
^ v≤TJ<\23<
`
^ <
Explanation
i pops the top of the stack (by default 0) and stores it into the variable i. , reads an integer from stdin, then t pops it and stores it into variable t. T pushes to the stack the value of the variable t and v redirects the intruction pointer towards the end of the next line. < tells the instruction pointer to go left. 1 pushes 1 onto the stack and j stores it into the variable j, and ` prints a newline. Then v and > sends the instruction towards the right on the next line. I pushes the variable i's value onto the stack, ) increments it, i takes it and puts it into i, I pushes the value of i onto the stack and T brings onto the stack the number that was previously read to stdin. ‹ compares them and terminates the program if the grid has finished printing. Then it initializes j to 0 and sends the instruction pointer into a loop that increments j, then prints a space (\23 (pointer is currently going left, that's why it is reversed) pushes 32 (ASCII id of " ") and prints it) (32\ is used instead of " "~ because it is one character shorter), prints j if j is lower than i, or else prints i. When j is greater or equal to t, it exits the loop, prints a new line and goes back into the outer loop.
In the code, the t variable contains the number read from stdin, i is the current line number, and j is the current character number.
Python 3, 67 bytes
lambda n:[print(*(max(y,x)+1 for y in range(n))) for x in range(n)]
Haskell, 28 bytes
f n=(<$>[1..n]).max<$>[1..n]
Generates each row by taking the max of each element of [1..n] and a fixed value equal to the row number.
map(max 3)[1,2,3,4,5] = [3,3,3,4,5]
We use <$> as an infix synonym for map.
It would be nice to reuse the <$>[1..n], but it runs into type-checking issues
26 bytes, doesn't work
f n|q<-(<$>[1..n])=q$q.max
If a 1D output is allowed, we can do:
25 bytes
f n=max<$>[1..n]<*>[1..n]
05AB1E, 6 bytes
Lã€àsä
Try it online! Outputs as a 2D list. Link includes a footer to format the output.
Lã€àsä # full program
€ # list of...
à # maximum...
€ # s...
à # of...
€ # each element of...
ã # list of distinct combinations of two elements in...
L # [1, 2, 3, ...,
# ..., implicit input...
L # ]...
ã # repeated twice...
ä # split into pieces of length...
s # implicit input
# implicit output
PHP -F, 80 69 bytes
for(;++$i<=$argn;)echo str_pad('',$i-1,$i).join(range($i,$argn))."
";
Quite naive solution, but I think it can be golfed more..
EDIT: saved 11 bytes by having a shorter str_pad so that the join(range( is no longer conditional
JavaScript (Node.js), 56 bytes
n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>x>y++?-~x:y))
Outputs an array of arrays.
Thanks to Shaggy for -5 bytes
Thanks to Arnauld for further -2 bytes
JavaScript (Node.js), 71 bytes
n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>x>y++?-~x:y).join``).join`
`
Outputs string
History
n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>-~x>++y?-~x:y)) // 58
n=>[...m=Array(n)].map((_,a)=>[...m].map((_,b)=>-~a>++b?-~a:b)) // 63
n=>[...(m=Array(n))].map((_,a)=>[...m].map((_,b)=>-~a>++b?-~a:b)) // 65
n=>[...(m=Array(n)).keys()].map(a=>[...m.keys()].map(b=>-~a>++b?-~a:b)) // 71
C# (Visual C# Interactive Compiler), 70 bytes
a => new int[a][].Select((x,p)=>new int[a].Select((y,q)=>p>q?p+1:q+1))
I'm sure there is a shorter way to do this, but I can't find it.
Java (JDK), 84 bytes
n->{for(int i=0;i<n*n;)System.out.printf("%d%c",i%n>i/n?i%n+1:i/n+1,++i%n<1?13:32);}
Alternative, 84 bytes too
n->{for(int i=1,j=0;i<=n;)System.out.printf("%d%c",i>++j?i:j,(j%=n)<1?13+i-i++:32);}
Japt, 5 bytes
Outputs a 2D-array.
õ@õwX
õ@õwX :Implicit input of integer U
õ :Range [1,U]
@ :Map each X
õ : Range [1,U]
wX : Max of each with X
Or, if outputting a 1D-array is allowed:
Japt, 4 bytes
õ ïw
õ ïw :Implicit input of integer U
õ :Range [1,U]
ï :Cartesian product with itself
w :Reduce each pair by Max
Vyxal, 6 bytes
ƛ£⁰ƛ¥∴
Try it Online! Returns an array of arrays
ƛ # (1...input) Map...
£ # Push to register
⁰ƛ # (1...input) Map...
¥ # Register
∴ # Maximum of the two
Or, for grid output, 10 bytes:
ƛ£⁰ƛ¥∴;Ṅ;⁋
ƛ ; # (1...input) Map...
£ # Push to register
⁰ƛ ; # (1...input) Map...
¥ # Push register
∴ # Max
Ṅ # Joined by spaces
⁋ # Joined by newlines.
Factor, 35 bytes
[ [1,b] dup [ max ] cartesian-map ]
cartesian-map Takes two sequences (in this case, both are [1, n]) and applies a quotation to each pair of elements, resulting in a matrix. In effect, we are mapping max over a coordinate matrix.
Charcoal, 11 bytes
NθEθ⭆θ⊕⌈⟦ιλ
Try it online! Link is to verbose version of code. Explanation:
Nθ First input as an integer
θ Input
E Map over implicit range (0-indexed)
θ Input
⭆ Map over implicit range and join
ι Outer index
λ Inner index
⌈⟦ Maximum of all of the above
⊕ Incremented
Implicitly print
Would be 14 bytes if spacing was desired (13 bytes if a leading space was acceptable).
C (clang), 75 77 bytes
Added 2 bytes for a bug fix kindly provided by dingledooper.
i;j;k;f(n){for(i=0;n/++i;puts(""))for(j=i,k=0;n/++k;j+=k>=i)printf("%d ",j);}
K (ngn/k), 10 bytes
{x|\x}1+!:
Somewhat surprisingly, using a seeded scan x|\x returns the same results as an each-right (x|/:x) or each-left (x|\:x).
1+!:generate1..nfrom the (implicit) input{x|\x}set up a scan, seeded with the input, run over the input
Posix SH + sed, 99 97 96 bytes
seq -s\ $1|sed -nE ':t
p
s/((.)( \2)*) (.)/\1=\4/
/(.)=\1/q
:r
s/ ?(.)=(.)/=\2 \2/
tr
s/=//
tt'
or, 99 bytes with looping (ugh):
seq -s\ $1|sed -nE ':t
p
s/(.) /\1=/
:l
s/(.)=\1 ?/\1 \1=/
tl
:r
s/ ?(.)=(.)/=\2 \2/
tr
s/^=//
tt'
With an earlier gnu sed version, :t and tt can be replaced with : and t, respectively, saving two bytes for each solution (I think).
Takes input in $1. Outputs exactly as in the question (but with a trailing newline I think).
Explanation
seq -s\ $1 \ # Collect the numbers 1..$1 with space as
\ # the -separator.
|sed -nE '...' # Replace it. The -E is for -Extended Regular
# Expressions, which allow less escaping.
# The -n is for -no printing by default.
:t # Label for "t"op.
p # "p"rint the line.
s/((.)( \2)*) (.)/\1=\4/ # Mark the end of the first run of
# equal elements followed by a number.
# 4 4 4 4 5 6 7 8 9
# => 4 4 4 4=5 6 7 8 9
# 9 9 9 9 9 9 9 9 9
# => 9 9 9 9 9 9 9 9=9
/(.)=\1/q # If the marked element and its successor
# are the same (like in the example above
# with all nines), "q"uit, don't process
# any more lines.
:r # Start "r"eplacement loop.
s/ ?(.)=(.)/=\2 \2/ # For any characters a, b, replace a=b
# with =b b. examples:
# 4 4 4 4=5 6 7 8 9
# => 4 4 4=5 5 6 7 8 9
# => 4 4=5 5 5 6 7 8 9
tr # "t"est if there was a replacement made.
# if there was, do another replacement
# loop iteration.
s/=// # At this point, the cursor must have
# reached the beginning of the line
# so no replacement could be made.
# Remove the =
tt # Go to the top again, where this row
# will be printed, and a new one will be
# started.
JavaScript (Node.js), 85 bytes
n=>[...(A=Array)(n)].map((_,i)=>[...A(i++).fill(i),...[...A(n-i+1)].map((u,j)=>i+j)])
In the form of an array. Since JS doesn't have the convenient list.__mul__ and list.__add__ operations, this takes up quite some space.
JavaScript (Node.js), 90 bytes
f=(n,i=0)=>n-i?`${i+1} `.repeat(i)+[...Array(n-i++)].map((_,j)=>i+j).join` `+`
`+f(n,i):''
As a string.
Python 2, 50 bytes
lambda x:[[i+1]*i+range(i+1,x+1)for i in range(x)]
-2 bytes thanks to dingledooper
J, 9 bytes
1+>./~@i.
Explanation
1+>./~@i. input: n
i. range from 0 to n-1 inclusive
/~ table:
>. max(n, n)
1+ add 1 to each
APL (Dyalog Unicode), 11 bytes
{(⍳⍵)∘.⌈⍳⍵}
Anonymous prefix function
-2 bytes thanks to @Adam in chat
APL (Dyalog Unicode), 5 bytes (SBCS)
Anonymous tacit prefix function.
∘.⌈⍨⍳
∘.⌈⍨ maximum-table for
⍳ integers 1 through the argument
Jelly, 2 bytes
»þ
Outputs an n x n matrix. If the output format is required, 4 bytes
This is simply a table of maximums. For each cell \$A_{i,j}\$ in the output, the result is equal to \$\max(i,j)\$. The 4 byte answer has ` to reuse the argument, and G to format the output as a grid.

