31
\$\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

55 Answers 55

1
2
2
\$\begingroup\$

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.

Example5

Example8

\$\endgroup\$
3
  • \$\begingroup\$ You can drop a byte by removing the terminal " \$\endgroup\$ Commented Mar 9, 2022 at 23:31
  • 1
    \$\begingroup\$ @TaylorRaine I was specifically thinking of you when I figured out I could drop .Formula after the resize and I was so excited to have an answer where you wouldn't be able to point out what should have been an obvious optimization. One day, Taylor. One day... \$\endgroup\$ Commented Mar 10, 2022 at 13:18
  • \$\begingroup\$ XD You'll get there, I'm sure of it! And it's not like I don't make those same mistakes myself, lol :P \$\endgroup\$ Commented Mar 10, 2022 at 21:29
2
\$\begingroup\$

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``

Try it online!

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

Perl 5, 39 bytes

//,say map{$",$_<$'?$':$_}@;for@;=1..<>

Try it online!

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

APL (Dyalog Unicode), 11 bytes

{(⍳⍵)∘.⌈⍳⍵}

Anonymous prefix function

-2 bytes thanks to @Adam in chat

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can use ∘.⌈⍨⍳⍵ instead of (⍳⍵)∘.⌈⍳⍵, buuut then it's the same as Adam's answer :/ \$\endgroup\$
    – user
    Commented Jun 10, 2021 at 17:12
  • 1
    \$\begingroup\$ @user thanks but Adam already ninja'd the suggestion :P (in chat) \$\endgroup\$
    – Wasif
    Commented Jun 10, 2021 at 17:14
  • \$\begingroup\$ Oh, I see. You don't need the f←, by the way. \$\endgroup\$
    – user
    Commented Jun 10, 2021 at 17:15
  • \$\begingroup\$ @user oh sorry (filler) \$\endgroup\$
    – Wasif
    Commented Jun 10, 2021 at 17:15
  • 2
    \$\begingroup\$ 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: ⍳∘.⌈⍳ \$\endgroup\$
    – Adám
    Commented Jun 10, 2021 at 17:40
1
\$\begingroup\$

Vyxal, 15 bytes

ɾƛnS:I*fvI⁰ɾnȯJ

Try it Online!

Wish if we had maximum table....

\$\endgroup\$
0
1
\$\begingroup\$

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

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The core dump is likely because you didn't provide an argument for puts(). \$\endgroup\$ Commented Jun 10, 2021 at 18:51
  • \$\begingroup\$ @dingledooper I'm blind - thanks! :D \$\endgroup\$
    – Noodle9
    Commented Jun 10, 2021 at 19:24
1
\$\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\$
1
\$\begingroup\$

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

Try it online!

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

Try it online!

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

Husk, 4 bytes

´ṪYḣ

Try it online!

Outputs as a 2d matrix.

´       # argdup: apply a funciton 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.

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

PHP -F, 80 69 bytes

for(;++$i<=$argn;)echo str_pad('',$i-1,$i).join(range($i,$argn))."
";

Try it online!

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

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

Python, 52 bytes

lambda a="123456789":[a[n]*n+a[n:]for n in range(9)]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ It still dosen't take input, please re-read the question and fix it like this \$\endgroup\$
    – Wasif
    Commented Sep 30, 2021 at 16:12
1
\$\begingroup\$

Vyxal 2.6.0 r, 4 bytes

ẊvGẇ

Try it Online!

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

Pari/GP, 26 bytes

n->matrix(n,,i,j,max(i,j))

Try it online!

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

Pyth, 12 bytes

VSQm?>dNdNSQ

Try it online!

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

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

Thunno 2, 4 bytes

RDȷ§

Try it online!

Explanation

RDȷ§  # Implicit input
R     # Push [1..input]
 D    # Duplicate
  ȷ   # Outer product over:
   §  #  Dyadic maximum
      # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Uiua, 6 bytes

⊞↥.+1⇡

Try it!

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

Vyxal, 2 bytes

ǒ∴

Try it Online!

Wish if we had maximum table....

Wish no more!

Quite literally table by maximum

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

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

Try it online!

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

JavaScript (Node.js), 52 bytes

n=>(g=(i,j)=>i?[...g(i-1,j),j?i<j?j:i:g(n,i)]:[])(n)

Try it online!


JavaScript (Node.js), 55 bytes

n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>1+(x+=y>x)))

Try it online!

From user100752(deleted)'s answer

Alternate

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
1
0
\$\begingroup\$

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.


\$\endgroup\$
0
\$\begingroup\$

Arturo, 36 bytes

$[n][map 1..n'x[++repeat x x-1x..n]]

Try it

$[n][               ; a function taking an argument n
  map 1..n'x[       ; map over the range 1..n; assign current value to x
    ++              ; append
    repeat x x-1    ; a block containing x x-1 times
    x..n            ; the range x..n
  ]                 ; end map
]                   ; end function
\$\endgroup\$
0
\$\begingroup\$

Perl 5 + -M5.10.0 -na, 37 bytes

$,=$";say+($_)x$_,$_+1.."@F"for 1..$_

Try it online!

\$\endgroup\$
1
2

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.