Matrix in Range

18

0

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

user73398

Posted 2017-08-28T20:43:41.480

Reputation:

Answers

10

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!

Giuseppe

Posted 2017-08-28T20:43:41.480

Reputation: 21 077

+1 nice, I was thinking about outer but hadn't quite got there – MickyT – 2017-08-28T22:00:05.240

7

Mathematica, 61 46 bytes

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

thanx @alephalpha for -15 bytes

J42161217

Posted 2017-08-28T20:43:41.480

Reputation: 15 931

ArrayFlatten@{{Array[+##-1&,{#,#}],2#},{2#,2#}}& – alephalpha – 2017-08-29T03:47:27.647

+##-1& can just be ##-1& and you can use infix Array: ArrayFlatten@{{##-1&~Array~{#,#},2#},{2#,2#}}& – ngenisis – 2017-08-29T20:27:29.450

5

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)

Luis Mendo

Posted 2017-08-28T20:43:41.480

Reputation: 87 464

5

Haskell, 48 bytes

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

Try it online!

jferard

Posted 2017-08-28T20:43:41.480

Reputation: 1 764

5

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!

rahnema1

Posted 2017-08-28T20:43:41.480

Reputation: 5 435

4

Jelly, 11 bytes

+þḶ;€Ḥ;ḤzḤG

Try it online!

Leaky Nun

Posted 2017-08-28T20:43:41.480

Reputation: 45 011

Was just about to post byte for byte without the G which is superfluous to requirements – Jonathan Allan – 2017-08-28T21:04:55.240

Nice use of outer product – Zacharý – 2017-08-28T21:05:44.810

@JonathanAllan another solution uses ;€ḤZ;€Ḥ... – Leaky Nun – 2017-08-28T21:13:43.150

...and yet another is Ḷ;Ḥ©µ+þ‘«® ;p – Jonathan Allan – 2017-08-28T21:48:43.233

1@JonathanAllan and another uses +€Ḷ;Ḥṁ€;€Ḥ :p – Erik the Outgolfer – 2017-08-29T10:16:55.993

The G isn't needed, is it? – Zacharý – 2017-08-29T15:18:28.857

4

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>

Neil

Posted 2017-08-28T20:43:41.480

Reputation: 95 035

4

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

Jakob

Posted 2017-08-28T20:43:41.480

Reputation: 2 428

You can golf a byte by starting i=0 and putting the ++ at [c=i++%p]. – Kevin Cruijssen – 2017-08-29T11:52:54.293

3

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!

totallyhuman

Posted 2017-08-28T20:43:41.480

Reputation: 15 378

1As usual, *(n+1) is *-~n. – Mr. Xcoder – 2017-08-28T20:54:15.450

1Some more obfuscation for 61 bytes – Mr. Xcoder – 2017-08-28T20:58:49.103

If you want to adopt the Python 3 version too, the shortest I could get is 64 bytes

– Mr. Xcoder – 2017-08-28T21:15:49.727

The 76 bytes version can be reduced to 72 bytes

– Halvard Hummel – 2017-08-29T08:14:50.933

3

Pyth, 18 bytes

+m+rd+QdyQSQ]*]yQh

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

Test Suite.

"Pretty print" Test Suite.

Mr. Xcoder

Posted 2017-08-28T20:43:41.480

Reputation: 39 774

2

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!

Mr. Xcoder

Posted 2017-08-28T20:43:41.480

Reputation: 39 774

44 bytes. – totallyhuman – 2017-08-28T21:05:01.893

@totallyhuman thanks. – Mr. Xcoder – 2017-08-28T21:05:35.623

2

Japt, 17 16 bytes

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

Try it online!

Oliver

Posted 2017-08-28T20:43:41.480

Reputation: 7 160

2

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

MickyT

Posted 2017-08-28T20:43:41.480

Reputation: 11 735

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) – Giuseppe – 2017-08-28T22:00:29.537

1`\`` works for escaping backticks in a code block delimited by backticks @Giuseppe – Cyoce – 2017-08-29T03:07:22.647

2

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!

officialaimm

Posted 2017-08-28T20:43:41.480

Reputation: 2 739

1

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

Zacharý

Posted 2017-08-28T20:43:41.480

Reputation: 5 710

1

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!

cleblanc

Posted 2017-08-28T20:43:41.480

Reputation: 3 360

i<n+1 is i<=n (117 bytes). – Mr. Xcoder – 2017-08-28T21:20:52.007

@Mr.Xcoder Thanks ! – cleblanc – 2017-08-28T21:22:34.217

1

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    */

fergusq

Posted 2017-08-28T20:43:41.480

Reputation: 4 867

1

><>, 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+\
             \                   /

Sasha

Posted 2017-08-28T20:43:41.480

Reputation: 431

0

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

dzaima

Posted 2017-08-28T20:43:41.480

Reputation: 19 048

0

Jelly, 14 bytes

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

Try it online!

Zacharý

Posted 2017-08-28T20:43:41.480

Reputation: 5 710

0

Perl 5, 56 bytes

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

Try it online!

Xcali

Posted 2017-08-28T20:43:41.480

Reputation: 7 671

0

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!

Jonah

Posted 2017-08-28T20:43:41.480

Reputation: 8 729

Heh, my APL answer and your answer have the same byte count! Can you add an explanation please? – Zacharý – 2017-08-30T02:06:49.967

@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. – Jonah – 2017-08-30T17:29:52.867

0

Kotlin, 59 bytes

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

Try it online.

Valentin Michalak

Posted 2017-08-28T20:43:41.480

Reputation: 211

0

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

Joshua

Posted 2017-08-28T20:43:41.480

Reputation: 231

0

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

Emigna

Posted 2017-08-28T20:43:41.480

Reputation: 50 798

0

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

Mego

Posted 2017-08-28T20:43:41.480

Reputation: 32 998

0

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

Chris

Posted 2017-08-28T20:43:41.480

Reputation: 31

0

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!

Zongor

Posted 2017-08-28T20:43:41.480

Reputation: 51