19
\$\begingroup\$

The Challenge

Given an integer n>0 output a n+1 X n+1 matrix containing all integers from 1 to 2n as shown in the test cases bellow

Test Cases

n=1  

1  2  
2  2

n=2

1   2   4  
2   3   4  
4   4   4

n=5  

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

n=10  

1   2   3   4   5   6   7   8   9   10  20  
2   3   4   5   6   7   8   9   10  11  20  
3   4   5   6   7   8   9   10  11  12  20  
4   5   6   7   8   9   10  11  12  13  20  
5   6   7   8   9   10  11  12  13  14  20  
6   7   8   9   10  11  12  13  14  15  20  
7   8   9   10  11  12  13  14  15  16  20  
8   9   10  11  12  13  14  15  16  17  20  
9   10  11  12  13  14  15  16  17  18  20  
10  11  12  13  14  15  16  17  18  19  20  
20  20  20  20  20  20  20  20  20  20  20  

I think that the pattern is pretty easy, so let's see who will give the shortest answer in bytes.
This is

Rules

Input must be an integer (1-indexed)

Output can be a matrix (as shown in the test cases) or a list of lists

\$\endgroup\$

28 Answers 28

10
\$\begingroup\$

R, 53 bytes

function(n)rbind(cbind(outer(1:n,1:n,`+`)-1,2*n),2*n)

Uses the outer "product" to generate all sums of the range 1,...,n as a matrix, subtracts 1 from each, then binds 2*n as a column on the right and a row on the bottom, recycling as needed, and returns a matrix as the result.

Try it online!

R, 78 bytes

More naive implementation.

function(n){m=matrix(2*n,n+1,n+1)
for(i in seq(n))m[1:n,i]=(0:(2*n))[1:n+i]
m}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ +1 nice, I was thinking about outer but hadn't quite got there \$\endgroup\$
    – MickyT
    Commented Aug 28, 2017 at 22:00
7
\$\begingroup\$

Mathematica, 61 46 bytes

ArrayFlatten@{{##-1&~Array~{#,#},2#},{2#,2#}}&

thanx @alephalpha for -15 bytes

\$\endgroup\$
2
  • \$\begingroup\$ ArrayFlatten@{{Array[+##-1&,{#,#}],2#},{2#,2#}}& \$\endgroup\$
    – alephalpha
    Commented Aug 29, 2017 at 3:47
  • \$\begingroup\$ +##-1& can just be ##-1& and you can use infix Array: ArrayFlatten@{{##-1&~Array~{#,#},2#},{2#,2#}}& \$\endgroup\$
    – user61980
    Commented Aug 29, 2017 at 20:27
5
\$\begingroup\$

MATL, 12 10 bytes

:&+q,!GEYc

Try it online!

Explanation

:       % Input n (implicit). Push range [1 2 ... n]
&+      % matrix of pairwise additions
q       % Subtract 1
,       % Do twice
  !     %   Transpose
  GE    %   Push 2*n
  Yc    %   Concatenate that value to all rows, thus extending the matrix
        % End (implicit). Display (implicit)
\$\endgroup\$
5
\$\begingroup\$

Haskell, 48 bytes

f n=[[i..n-1+i]++[2*n]|i<-[1..n]]++[2*n<$[0..n]]

Try it online!

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

Octave, 38 37 35 bytes

@(n)[(x=1:n)+x'-1 z=~x'+2*n;z' 2*n]

Try it online!

or

@(n)~(k=blkdiag((x=1:n)+x'-1,0))*2*n+k

Try it online!

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

Jelly, 11 bytes

+þḶ;€Ḥ;ḤzḤG

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ Was just about to post byte for byte without the G which is superfluous to requirements \$\endgroup\$ Commented Aug 28, 2017 at 21:04
  • \$\begingroup\$ Nice use of outer product \$\endgroup\$
    – Adalynn
    Commented Aug 28, 2017 at 21:05
  • \$\begingroup\$ @JonathanAllan another solution uses ;€ḤZ;€Ḥ... \$\endgroup\$
    – Leaky Nun
    Commented Aug 28, 2017 at 21:13
  • \$\begingroup\$ ...and yet another is Ḷ;Ḥ©µ+þ‘«® ;p \$\endgroup\$ Commented Aug 28, 2017 at 21:48
  • 1
    \$\begingroup\$ @JonathanAllan and another uses +€Ḷ;Ḥṁ€;€Ḥ :p \$\endgroup\$ Commented Aug 29, 2017 at 10:16
4
\$\begingroup\$

JavaScript (ES6), 64 bytes

f=
n=>[...Array(n+1)].map((_,i,a)=>a.map((_,j)=>n-i&&n-j?i-~j:n+n))
<input type=number min=0 oninput="t.innerHTML=f(+this.value).map(a=>`<tr>${a.map(b=>`<td>${b}</td>`).join``}</tr>`).join``"><table id=t>

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

Java 8, 99 bytes

Lambda from Integer to int[][] (e.g. Function<Integer, int[][]>). Surprisingly resistant to golfing.

n->{int p=n+1,o[][]=new int[p][p],i=0,r,c;while(i<p*p)o[r=i/p][c=i++%p]=r<n&c<n?r-~c:2*n;return o;}

Try It Online

Ungolfed lambda

n -> {
    int
        p = n + 1,
        o[][] = new int[p][p],
        i = 0,
        r, c
    ;
    while (i < p * p)
        o[r = i / p][c = i++ % p] =
            r < n & c < n ?
                r - ~c
                : 2 * n
        ;
    return o;
}

Acknowledgments

  • -1 byte thanks to Kevin Cruijssen
\$\endgroup\$
1
  • \$\begingroup\$ You can golf a byte by starting i=0 and putting the ++ at [c=i++%p]. \$\endgroup\$ Commented Aug 29, 2017 at 11:52
3
\$\begingroup\$

Python 2, 64 62 61 bytes

-3 bytes thanks to Mr. Xcoder.

lambda n:[range(i+1,i-~n)+[n*2]for i in range(n)]+[[n*2]*-~n]

Try it online!

I'm probably missing a key pattern though.

Python 2, 76 bytes

lambda n:[[[n*2,i-~j][n-i and n-j>0]for j in range(n+1)]for i in range(n+1)]

Try it online!

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

Pyth, 18 bytes

+m+rd+QdyQSQ]*]yQh

Maybe I am missing an obvious pattern (cc @totallyhuman)...

Test Suite.

"Pretty print" Test Suite.

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

Proton,  48  44 bytes

-4 bytes thanks to @totallyhuman!

n=>[(i+1..i-~n)+[n*2]for i:0..n]+[[n*2]*-~n]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 44 bytes. \$\endgroup\$ Commented Aug 28, 2017 at 21:05
  • \$\begingroup\$ @totallyhuman thanks. \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 28, 2017 at 21:05
2
\$\begingroup\$

R, 54 63 67 bytes

function(n)cbind(rbind(sapply(1:n-1,'+',1:n),2*n),2*n)

Try it online!

Thanks to @Guiseppe for the pointer for sapply and the 9 bytes

\$\endgroup\$
2
  • \$\begingroup\$ Actually, this approach can be improved as well: sapply(1:n-1,'+',1:n) but then it's a mere 1 byte more than using outer (the quotes are obviously backticks) \$\endgroup\$
    – Giuseppe
    Commented Aug 28, 2017 at 22:00
  • 1
    \$\begingroup\$ \` works for escaping backticks in a code block delimited by backticks @Giuseppe \$\endgroup\$
    – Cyoce
    Commented Aug 29, 2017 at 3:07
2
\$\begingroup\$

Japt, 17 16 bytes

õ_óU pU*2ÃpUô@*2

Try it online!

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

Recursiva, 50 bytes

  • Only 10 bytes shorter than python and thus it is official, Recursiva is not a golfing-language at all... It is an esolang though. :D
|{Ba+++++'PJ"	"W+Z~}B+~}'Va'+}'Va'AD'VaJ"	"W*;aADa

Try it online!

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

C (gcc), 119 116 115 107 bytes

i,j;f(n){for(;j<n;j++,printf("%d\n",2*n))for(i=0;i++<n;printf("%d\t",j+i));for(;j=i--;printf("%d\t",n*2));}

Try it online!

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

Röda, 44 bytes

f n{seq 1,n|[[seq(_,_1+n-1)]+2*n];[[2*n]*n]}

Try it online!

Explanation:

f n{seq 1,n|[[seq(_,_1+n-1)]+2*n];[[2*n]*n]}
f n{                                       } /* Function f(n)         */
    seq 1,n                                  /* Sequence 1..n         */
           |                                 /* For each _1:          */
              seq(_,_1+n-1)                  /*   Sequence _1.._1+n-1 */
             [             ]                 /*   As list             */
                            +2*n             /*   Append 2*n          */
            [                   ]            /*   Push to the stream  */
                                   [2*n]     /* List [2*n]            */
                                        *n   /* Multiplied by n       */
                                  [       ]  /* Push to the stream    */
\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 29 bytes

Requires ⎕IO←0

{(S,⍨1+¯1 ¯1↓∘.+⍨⍳⍵+1)⍪S←2×⍵}

Try it online!

How?

  • 1+¯1 ¯1↓∘.+⍨⍳⍵+1 the upper-left portion of the array
  • (S,⍨...)⍪S←2×⍵ the corner
\$\endgroup\$
1
\$\begingroup\$

><>, 84+2 Bytes

+2 for -v flag

Prints with tabs between values, and newlines between rows. Also prints a trailing tab on the last line.

Try it online

1:r:&r&)?\0:r:&r&(?\~$:@2*nao1+!
a0./:r:0~<.17+1o9\ \$:@$:@+n9o1+
   \&r&)?;$:@2*n /

Pre-golfing

1>:r:&r&)?\0>    :r:&r&(?\~$:@2*nao1+\
            \+1o9n+@:$@:$/
 \                                   /
          \~0>:r:&r&)?;$:@2*n9o1+\
             \                   /
\$\endgroup\$
0
\$\begingroup\$

SOGL V0.12, 22 bytes

∫HA.∫a+}.«¹}¹.I.«r∙rο+

Try it Here!
leaves output on the stack, which you can view in the console

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

Jelly, 14 bytes

R+’r@R;€Ḥz0;€Ḥ

Try it online!

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

Perl 5, 56 bytes

$,=$";say($_..$_+$"-1,$"*2)for 1..($"=<>);say(($"*2)x$")

Try it online!

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

Kotlin, 59 bytes

Array(n+1){c->Array(n+1){r->when(n){c,r->n*2;else->1+c+r}}}

Try it online.

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

Clojure, 153 135 bytes

List of lists? Yay, Lisp

(fn[n](loop[r[] i 0 d (* 2 n)](if(= i n)(conj r(conj(repeat n d)d))(recur(conj r(conj(vec(map #(+ i %)(range 1(inc n))))d))(inc i)d))))

Ungolfed:

(defn a[n]
  (loop [r[] i 0 d (* 2 n)]
    (if(= i n)
      (conj r(conj(repeat n d)d))
      (recur
        (conj r
            (conj (vec (map #(+ i %)(range 1(inc n)))) d))
        (inc i)
        d))))

Anonymous function that takes the input as argument and returns a list of lists.

Output of n=5:

[[1 2 3 4 5 10] [2 3 4 5 6 10] [3 4 5 6 7 10] [4 5 6 7 8 10] [5 6 7 8 9 10] (10 10 10 10 10 10)]
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 17 bytes

FLN+I·¸«ˆ}·¸I>.׈

Try it online!

Explanation

F                   # for N in [0 ... input-1]
 L                  # push range [1 ... input]
  N+                # add N to each
    I·¸«            # append input*2
        ˆ           # add to global list
         }          # end loop
          ·¸        # push [input*2]
            I>.×    # repeat it input+1 times
                ˆ   # add to global list
                    # implicitly output global list
\$\endgroup\$
0
\$\begingroup\$

J, 29 bytes

(}:@(][\1+i.@+:),]#+:),.>:#+:

ungolfed

(}:@(] [\ 1+i.@+:) , ]#+:) ,. >:#+:

explanation

(}:@(] [\ 1+i.@+:)                   NB. make the inner part of the matrix
          1+i.@+:                      NB. 1..2*n, where n is the input
    (] [\ 1+i.@+:)                     NB. fork: infixes (sliding window) of length n, over 1..2*n
(}:@                                   NB. remove last element
                   , ]#+:)           NB. add a row of 2*n to the end
                           ,. >:#+:  NB. add a column of 2*n to entire result above

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Heh, my APL answer and your answer have the same byte count! Can you add an explanation please? \$\endgroup\$
    – Adalynn
    Commented Aug 30, 2017 at 2:06
  • \$\begingroup\$ @Zacharý, updated. fwiw, this could probably be golfed at least a bit further by someone like myself, and perhaps by additional 10+ bytes by a J expert. \$\endgroup\$
    – Jonah
    Commented Aug 30, 2017 at 17:29
0
\$\begingroup\$

Actually, 23 bytes

;;Rnkp@;r♀+@;τ;(♀q)@α@q

Try it online!

Explanation:

;;Rnkp@;r♀+@;τ;(♀q)@α@q
;;                       two copies of input
  R                      range(1, input+1)
   n                     copy input times
    kp@                  push stack to list, remove first element
       ;r                push range(input)
         ♀+              pairwise addition (add the value in the range to each value in the corresponding list)
           @;            duplicate input again
             τ;          input*2, duplicate that
               (♀q)      append input*2 to each list
                   @α@q  append a row of input*2
\$\endgroup\$
0
\$\begingroup\$

Clojure v1.8, 97 bytes

#(conj(mapv(fn[i](conj(vec(range i(+ % i)))(* 2 %)))(range 1(inc %)))(vec(repeat(inc %)(* 2 %))))

Try it online!

Explanation

(range 1(inc %))                           Numbers from 1 to 'n'
(mapv ... (range 1(inc %)))                For each one of these numbers
(fn[i](conj(vec(range i(+ % i)))(* 2 %)))  Create the numbers from 'i' to (n+i-1), convert to vector and insert '2*n' to the vector
#(conj ... (vec(repeat(inc %)(* 2 %))))    Insert to the previous vector a vector of repeated '2*n's
\$\endgroup\$
0
\$\begingroup\$

Fortran (GFortran), 93, 82 bytes

SUBROUTINE M(n)
PRINT*,(CHAR(10),(i-1+j,i=1,n),2*n,j=1,n)
PRINT*,(2*n,j=1,n+1)
END

Try it online!

\$\endgroup\$

Your Answer

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