# Generate this number table

Given an integer $$\1 < n < 10 \$$ generate a table like below.

For $$\n = 5\$$,

1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5


For $$\n = 8\$$,

1 2 3 4 5 6 7 8
2 2 3 4 5 6 7 8
3 3 3 4 5 6 7 8
4 4 4 4 5 6 7 8
5 5 5 5 5 6 7 8
6 6 6 6 6 6 7 8
7 7 7 7 7 7 7 8
8 8 8 8 8 8 8 8


Shortest code wins! Trailing whitespace allowed

Output format isn't strict, you can output a 2D list/matrix in any reasonable format.

• Welcome to Code Golf and nice first question! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main. Can the output be a list of lists/matrix, or does it have to be formatted like that? If the formatting is required, what should be the output for $n = 11$? – caird coinheringaahing Jun 10 at 16:58
• What are the permitted output formats? – Shaggy Jun 10 at 17:08
• Related – caird coinheringaahing Jun 10 at 18:34
• @cairdcoinheringaahing, I'm actually pretty sure we've had it with numbers, too, but I can't find it. – Shaggy Jun 10 at 21:30
• @BadCoder, what about a 1D-array, like so? – Shaggy Jun 11 at 8:45

# Jelly, 2 bytes

»þ


Try it online!

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.

# APL (Dyalog Unicode), 5 bytes (SBCS)

Anonymous tacit prefix function.

∘.⌈⍨⍳


Try it online!

∘.⌈⍨ maximum-table for

⍳integers 1 through the argument

# Factor, 35 bytes

[ [1,b] dup [ max ] cartesian-map ]


Try it online!

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.

# Wolfram Language (Mathematica), 16 bytes

Max~Array~{#,#}&


Try it online!

## <>^v, 93 89 bytes

i,tTv
vj1<
>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.

run online

New contributor
astroide is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

# J, 9 bytes

1+>./~@i.


Try it online!

## Explanation

1+>./~@i.    input: n
i.    range from 0 to n-1 inclusive
/~       table:
>.            max(n, n)


# 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:

ƛ£⁰ƛ¥∴;Ṅ;⁋


Try it Online!

ƛ       ;  # (1...input) Map...
£         # Push to register
⁰ƛ  ;    # (1...input) Map...
¥      # Push register
∴     # Max
Ṅ   # Joined by spaces
⁋ # Joined by newlines.


# Julia, 19 bytes

!n=max.((1:n)',1:n)


Try it online!

# K (ngn/k), 10 bytes

{x|\x}1+!:


Try it online!

Somewhat surprisingly, using a seeded scan x|\x returns the same results as an each-right (x|/:x) or each-left (x|\:x).

• 1+!: generate 1..n from the (implicit) input
• {x|\x} set up a scan, seeded with the input, run over the input

# R, 30 bytes

function(n)outer(1:n,1:n,pmax)


Try it online!

# Octave, 19 bytes

@(n)max([1:n]',1:n)


Try it online!

• For Octave you can actually do this to save 2 bytes: @(n)max(x=1:n,x') – elementiro Jun 11 at 12:00

# Stax, 3 bytes

╗ìΦ


Run and debug it

# Python 3, 49 bytes

f=lambda x:x*[x]and[a+[x]for a in f(x-1)]+[[x]*x]


Try it online!

# Python 2, 48 bytes

r=range(1,input()+1)
for x in r:print[x]*x+r[x:]


Try it online!

Prints a list for each row.

param($a)1..$a|%{"$(,$_*($_-1)+$_..$a)"}  Try it online! # 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. Try it online! • The first 2 spaces are unnecessary and the first array doesn't need to be multidimentional, saving 4 bytes. – raznagul Jun 11 at 11:07 # Python 3, 67 bytes lambda n:[print(*(max(y,x)+1 for y in range(n))) for x in range(n)]  Try it online! New contributor shoes is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. # APL (Dyalog Unicode), 11 bytes {(⍳⍵)∘.⌈⍳⍵}  Anonymous prefix function -2 bytes thanks to @Adam in chat • You can use ∘.⌈⍨⍳⍵ instead of (⍳⍵)∘.⌈⍳⍵, buuut then it's the same as Adam's answer :/ – user Jun 10 at 17:12 • @user thanks but Adam already ninja'd the suggestion :P (in chat) – Wasif Jun 10 at 17:14 • Oh, I see. You don't need the f←, by the way. – user Jun 10 at 17:15 • @user oh sorry (filler) – Wasif Jun 10 at 17:15 • How to get to a solution as shot as, but different from, mine: Notice that you have two "outer" function applications (⍳ and ⍳), with their results being the arguments to an "inner" dyadic function ∘.⌈. This is perfect for a fork: ⍳∘.⌈⍳ – Adám Jun 10 at 17:40 # Vyxal, 15 bytes ɾƛnS:I*fvI⁰ɾnȯJ  Try it Online! Wish if we had maximum table.... # Posix SH + sed, 9997 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.


# C (gcc), 65 bytes

i,j;f(n){for(i=1;n/i;)printf(j/n?j=!++i,"%d\n":"%d ",i>++j?i:j);}


Try it online!

# Ruby, 35 bytes

->x{(1..x).map{|y|[y]*y+[*y+1..x]}}


Try it online!

# Japt, 5 bytes

Outputs a 2D-array.

õ@õwX


Try it

õ@õ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


Try it

õ ïw     :Implicit input of integer U
õ        :Range [1,U]
ï      :Cartesian product with itself
w     :Reduce each pair by Max

• so close to having õwõ but not quite :( – hyper-neutrino Jun 10 at 17:03
• @hyper-neutrino, wouldn't be a valid Japt programme, anyway. – Shaggy Jun 10 at 17:09

# JavaScript (Node.js), 56 bytes

n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>x>y++?-~x:y))


Try it online!

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



Try it online!

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

• 58 bytes – Shaggy Jun 10 at 21:36
• 56 bytes (based on Shaggy's version) – Arnauld Jun 10 at 21:58
• Damn, @Arnauld, how'd I miss that? I blame being back behind my bar for the first time in 6 months; I'm clearly out of practice at "bar-golfing"! – Shaggy Jun 10 at 23:16

f n=(<$>[1..n]).max<$>[1..n]


Try it online!

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


Try it online!

If a 1D output is allowed, we can do:

25 bytes

f n=max<$>[1..n]<*>[1..n]  Try it online! # Python 3, 51 49 bytes lambda n:mgrid[:n,:n].max(0)+1 from numpy import*  Try it online! # Python 2, 50 bytes lambda x:[[i+1]*i+range(i+1,x+1)for i in range(x)]  Try it online! -2 bytes thanks to dingledooper # JavaScript (Node.js), 85 bytes n=>[...(A=Array)(n)].map((_,i)=>[...A(i++).fill(i),...[...A(n-i+1)].map((u,j)=>i+j)])  Try it online! 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):''


Try it online!

As a string.

# 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);}


Try it online!

• The core dump is likely because you didn't provide an argument for puts(). – dingledooper Jun 10 at 18:51
• @dingledooper I'm blind - thanks! :D – Noodle9 Jun 10 at 19:24

# Charcoal, 11 bytes

ＮθＥθ⭆θ⊕⌈⟦ιλ


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

Ｎθ          First input as an integer
θ        Input
Ｅ         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).