# 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

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

• +1 nice, I was thinking about outer but hadn't quite got there Aug 28, 2017 at 22:00

# Mathematica, 61 46 bytes

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


thanx @alephalpha for -15 bytes

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

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


f n=[[i..n-1+i]++[2*n]|i<-[1..n]]++[2*n<$[0..n]]  Try it online! # Octave, 3837 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! # Jelly, 11 bytes +þḶ;€Ḥ;ḤzḤG  Try it online! • Was just about to post byte for byte without the G which is superfluous to requirements Aug 28, 2017 at 21:04 • Nice use of outer product Aug 28, 2017 at 21:05 • @JonathanAllan another solution uses ;€ḤZ;€Ḥ... Aug 28, 2017 at 21:13 • ...and yet another is Ḷ;Ḥ©µ+þ‘«® ;p Aug 28, 2017 at 21:48 • @JonathanAllan and another uses +€Ḷ;Ḥṁ€;€Ḥ :p Aug 29, 2017 at 10:16 ## 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> # 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 • You can golf a byte by starting i=0 and putting the ++ at [c=i++%p]. Aug 29, 2017 at 11:52 # Python 2, 6462 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! • As usual, *(n+1) is *-~n. Aug 28, 2017 at 20:54 • Some more obfuscation for 61 bytes Aug 28, 2017 at 20:58 • If you want to adopt the Python 3 version too, the shortest I could get is 64 bytes Aug 28, 2017 at 21:15 • The 76 bytes version can be reduced to 72 bytes Aug 29, 2017 at 8:14 # Pyth, 18 bytes +m+rd+QdyQSQ]*]yQh  Maybe I am missing an obvious pattern (cc @totallyhuman)... Test Suite. "Pretty print" Test Suite. # 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! • 44 bytes. Aug 28, 2017 at 21:05 • @totallyhuman thanks. Aug 28, 2017 at 21:05 # R, 54 6367 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 • 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) Aug 28, 2017 at 22:00 • \ works for escaping backticks in a code block delimited by backticks @Giuseppe Aug 29, 2017 at 3:07 # Japt, 17 16 bytes õ_óU pU*2ÃpUô@*2  Try it online! # 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! # C (gcc), 119116115 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! • Aug 28, 2017 at 21:20 • @Mr.Xcoder Thanks ! Aug 28, 2017 at 21:22 • 101 bytes Aug 5, 2020 at 20:48 # 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 */  # 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 # ><>, 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+\ \ /  # 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 # Jelly, 14 bytes R+’r@R;€Ḥz0;€Ḥ  Try it online! # Perl 5, 56 bytes $,=$";say($_..$_+$"-1,$"*2)for 1..($"=<>);say(($"*2)x$")


Try it online!

# Kotlin, 59 bytes

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


Try it online.

# 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)]


# 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


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

• @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. Aug 30, 2017 at 17:29

# 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


# 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


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