32
\$\begingroup\$

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.

\$\endgroup\$
12
  • 1
    \$\begingroup\$ 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\$? \$\endgroup\$ Commented Jun 10, 2021 at 16:58
  • 1
    \$\begingroup\$ @cairdcoinheringaahing 1 < n < 10 see edit please \$\endgroup\$
    – BadCoder
    Commented Jun 10, 2021 at 17:01
  • 8
    \$\begingroup\$ What are the permitted output formats? \$\endgroup\$
    – Shaggy
    Commented Jun 10, 2021 at 17:08
  • 4
    \$\begingroup\$ Related \$\endgroup\$ Commented Jun 10, 2021 at 18:34
  • 1
    \$\begingroup\$ @cairdcoinheringaahing, I'm actually pretty sure we've had it with numbers, too, but I can't find it. \$\endgroup\$
    – Shaggy
    Commented Jun 10, 2021 at 21:30

59 Answers 59

16
\$\begingroup\$

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.

\$\endgroup\$
9
\$\begingroup\$

APL (Dyalog Unicode), 5 bytes (SBCS)

Anonymous tacit prefix function.

∘.⌈⍨⍳

Try it online!

∘.⌈⍨ maximum-table for

integers 1 through the argument

\$\endgroup\$
8
\$\begingroup\$

Wolfram Language (Mathematica), 16 bytes

Max~Array~{#,#}&

Try it online!

\$\endgroup\$
7
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

<>^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.

run online

\$\endgroup\$
1
5
\$\begingroup\$

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)
1+           add 1 to each
\$\endgroup\$
5
\$\begingroup\$

R, 30 bytes

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

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Octave, 19 bytes

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

Try it online!

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

Python 3, 49 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Stax, 3 bytes

╗ìΦ

Run and debug it

\$\endgroup\$
4
\$\begingroup\$

PowerShell Core, 40 bytes

param($a)1..$a|%{"$(,$_*($_-1)+$_..$a)"}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

Uiua, 6 bytes

⊞↥.+1⇡

Try it!

⊞↥.+1⇡
     ⇡  # range
   +1   # increment
  .     # duplicate
⊞↥      # table by maximum
\$\endgroup\$
3
\$\begingroup\$

Julia, 19 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Ruby, 35 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ The first 2 spaces are unnecessary and the first array doesn't need to be multidimentional, saving 4 bytes. \$\endgroup\$
    – raznagul
    Commented Jun 11, 2021 at 11:07
3
\$\begingroup\$

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: 1 repeated 1-1 = 0 times, so empty string
  • Group 4 is repeated 3-1+1 = 3 times:
    1. Group 4 not previously matched, so followed by 2 repeated 1-1/3 = 1 time (the division is integer division)
    2. Previous value of group 4 is 2, so followed by 3 repeated 1-2/3 = 1 time
    3. Previous value of group 4 is 3, so followed by 4 repeated 1-3/3 = 0 times

Result: 1 2 3

Line 2:

  • Group 2: previous value is 1, so 2
  • Group 3: 2 repeated 2-1 = 1 time
  • Group 4 is repeated 3-2+1 = 2 times:
    1. Previous value of group 4 is , which is not a number, so followed by 3 repeated 1-2/3 = 1 time
    2. Previous value of group 4 is 3, so followed by 4 repeated 1-3/3 = 0 times

Result: 2 2 3

Line 3:

  • Group 2: previous value is 2, so 3
  • Group 3: 3 repeated 3-1 = 2 times
  • Group 4 is repeated 3-3+1 = 1 time:
    1. Previous value of group 4 is , which is not a number, so followed by 4 repeated 1-3/3 = 0 times

Result: 3 3 3

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Rust, 84 bytes

|n|(1..=n).fold("".into(),|a,i|(1..=n).fold(a,|b,j|format!("{}{} ",b,i.max(j)))+"
")

Try it online!

The output will be a String. The newline is given literally inside quotes, which saves one byte.

\$\endgroup\$
2
\$\begingroup\$

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).

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ so close to having õwõ but not quite :( \$\endgroup\$
    – hyperneutrino
    Commented Jun 10, 2021 at 17:03
  • \$\begingroup\$ @hyper-neutrino, wouldn't be a valid Japt programme, anyway. \$\endgroup\$
    – Shaggy
    Commented Jun 10, 2021 at 17:09
2
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 58 bytes \$\endgroup\$
    – Shaggy
    Commented Jun 10, 2021 at 21:36
  • 1
    \$\begingroup\$ 56 bytes (based on Shaggy's version) \$\endgroup\$
    – Arnauld
    Commented Jun 10, 2021 at 21:58
  • 1
    \$\begingroup\$ 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"! \$\endgroup\$
    – Shaggy
    Commented Jun 10, 2021 at 23:16
  • \$\begingroup\$ 55 \$\endgroup\$
    – l4m2
    Commented Jan 26, 2022 at 4:54
2
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 4 bytes. (I'm honestly surprised M works here tbh. ;p) \$\endgroup\$ Commented Dec 1, 2021 at 15:13
2
\$\begingroup\$

Haskell, 28 bytes

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!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 67 bytes

lambda n:[print(*(max(y,x)+1 for y in range(n))) for x in range(n)]

Try it online!

\$\endgroup\$
1
2
\$\begingroup\$

Python 3, 51 49 bytes

lambda n:mgrid[:n,:n].max(0)+1
from numpy import*

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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]
\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.