16
\$\begingroup\$

Given n=m^2, return a list of integers that do not border the m x m grid of integers from 1 to n.

Examples

n=1 (m=1)

Grid:

[1]

Return:

[]

n=4 (m=2)

Grid:

[1,2]
[3,4]

Return:

[]

n=9 (m=3)

Grid:

[1,2,3]
[4,5,6]
[7,8,9]

Return:

[5]

n=16 (m=4)

Grid:

[ 1, 2, 3, 4]
[ 5, 6, 7, 8]
[ 9,10,11,12]
[13,14,15,16]

Return:

[6,7,10,11]

For higher values of m, this answer does a great visualization.


Rules:

  • You may take in either m or n (where n = m*m).
    • If taking in n you are allowed to have undefined behavior where there exists no m for n (E.G. 15).
    • n > 0, m > 0: Both must be integer values.
  • The output may be as a 1D/2D array, matrix or whitespace delimited
  • The output must be in order from least to greatest.
    • If outputting as a matrix this means it must be as it would be in the grid.
  • This is , lowest byte-count wins.
\$\endgroup\$
  • \$\begingroup\$ Complete fault on my end, I read it incorrectly. \$\endgroup\$ – DevelopingDeveloper Feb 2 '18 at 21:46
  • 3
    \$\begingroup\$ @DevelopingDeveloper hey man, if I had a nickle for every time I did that I'd be able to buy a beer or two. \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 21:51
  • \$\begingroup\$ If outputting as a 2D array, can a single empty array be included in the result? \$\endgroup\$ – Shaggy Feb 3 '18 at 9:41

34 Answers 34

6
\$\begingroup\$

C, 50 bytes

i;f(m){for(i=m;++i<m*m-m;)i%m>1&&printf("%d ",i);}

Try it online!

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

Octave, 31 bytes

@(m)vec2mat(1:m*m,m--)(2:m,2:m)

Returns a matrix.

Try it online!

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ Nice! I've never come across the vec2mat function before. \$\endgroup\$ – Tom Carpenter Feb 3 '18 at 18:05
6
\$\begingroup\$

Octave, 26 bytes

@(m)find((t=[0:m-2 0])'*t)

The code defines an anonymous function that inputs m and outputs a (possibly empty) column vector.

Try it online!

Explanation

@(m)                          % Define anonymous function of m
          t=[0:m-2 0]         % Build row vector [0 1 2 ... m-2 0] and assign it
                              % to variable t
         (           )'       % Complex-conjugate transpose into a column vector
                       *t     % Matrix-multiply that column vector times the row
                              % vector t. This gives an m×m matrix with zeros in
                              % the border and nonzeros in the other entries.
    find(                )    % Linear indices of nonzero entries. The result is
                              % in increasing order
|improve this answer|||||
\$\endgroup\$
5
\$\begingroup\$

Jelly, 8 bytes

’Ṗ×+€ṖḊ€

A monadic link taking m and returning a list of lists (the inner rows).

Try it online!

How?

’Ṗ×+€ṖḊ€ - Link m                    e.g. 5
’        - decrement                      4
 Ṗ       - pop (implicit range of input)  [1,2,3]
  ×      - multiply by m                  [5,10,15]
     Ṗ   - pop m                          [1,2,3,4]
   +€    - add €ach                       [[6,7,8,9],[11,12,13,14],[16,17,18,19]]
      Ḋ€ - dequeue €ach                   [[7,8,9],[12,13,14],[17,18,19]]
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Didn't feel like doing the python one ;)? \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 21:51
4
\$\begingroup\$

Pure Bash, 49

The boring answer:

for((i=$1;i++<$1*$1-$1;));{ ((i%$1>1))&&echo $i;}

Try it online.


Or the interesting answer for 52:

(($1>2))&&eval echo \$[$1*{1..$[$1-2]}+{2..$[$1-1]}]

Try it online.

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

Haskell, 31 bytes

f m=[i|i<-[m..m*m-m],mod i m>1]

Try it online!

Math version:

f(m) = {i : i ∈ (m, m² - m), i mod m < 1}

:P

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Wait, this isn't valid, f(5) should be 7,8,9,12,13,14,17,18,19 \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 19:49
  • \$\begingroup\$ Wait, oops I'm a dumbass. \$\endgroup\$ – totallyhuman Feb 2 '18 at 19:54
4
\$\begingroup\$

R, 44 43 32 bytes

function(n)(x=n:(n^2-n))[x%%n>1]

Try it online!

Returns a vector.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Neat output format, is that by default how a matrix is output in R? \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 19:51
  • 1
    \$\begingroup\$ Yep, that's the print function for a matrix. \$\endgroup\$ – Giuseppe Feb 2 '18 at 19:51
  • 1
    \$\begingroup\$ You can omit the second m in matrix(1:m^2,m,m,T): matrix(1:m^2,m,,T) \$\endgroup\$ – JAD Feb 3 '18 at 13:26
  • \$\begingroup\$ @JAD yes, of course. Thanks. \$\endgroup\$ – Giuseppe Feb 5 '18 at 16:26
  • \$\begingroup\$ Nice one, what do you think about using scan()? You might save 2 bytes. Try it online! \$\endgroup\$ – Robert Hacken Feb 10 '18 at 12:40
3
\$\begingroup\$

Jelly, 8 bytes

sƽḊṖ$⁺€

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Using m one could do ²s⁸ḊṖ$⁺€ too. (I have also posted another m alternative method.) \$\endgroup\$ – Jonathan Allan Feb 2 '18 at 21:14
  • \$\begingroup\$ @JonathanAllan Already discovered that, but no byte saving over there, you can't remove the :( \$\endgroup\$ – Erik the Outgolfer Feb 2 '18 at 21:18
3
\$\begingroup\$

Proton, 28 bytes

k=>filter(u=>1<u%k,k..k*~-k)

Try it online!

Takes m as input.

How?

Filters the integers in [k, k2-k) that, when divided by k, yield a remainder higher than 1. This ensures that both ends are trimmed, because the first one yields 0 and the last one yields 1. It is also guaranteed to return a higher value for any valid integer, because they are consecutive.

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 31 bytes

Table[# i+j+1,{i,#-2},{j,#-2}]&

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Bash + GNU utilities, 35

seq $1 $[$1*$1-$1]|sed 1~$1d\;2~$1d

Try it online.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 9 bytes

LItä¦¨ε¦¨

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ LItä¦¨ε¦¨ is fine, the output can be a 2D array. \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 20:19
  • \$\begingroup\$ Oh, I should have read the spec better. Thanks for the heads-up! \$\endgroup\$ – Mr. Xcoder Feb 2 '18 at 20:19
2
\$\begingroup\$

Python 2, 44 bytes

lambda t:[k for k in range(t,~-t*t)if k%t>1]

Try it online!

I promise this is my last answer (to this challenge) today. Takes m as input.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Ruby, 32 bytes

->m{(m..m*m-m).reject{|e|e%m<2}}

Takes m, returns a one-dimensional array.

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

MATL, 8 bytes

:G\1>&*f

Input is m. Output is the numbers in increasing order.

Try it online!

Explanation

Consider input 4 as an example.

:     % Implicit input: m. Push range [1 2 ... m-1 m]
      % STACK: [1 2 3 4]
G\    % Modulo m, element-wise
      % STACK: [1 2 3 0]
1>    % Greater than 1, element-wise.
      % STACK: [0 1 1 0]
&*    % Matrix of pair-wise products
      % STACK: [0 0 0 0;
                0 1 1 0;
                0 1 1 0;
                0 0 0 0]
f     % Column vector of linear indices of nonzeros. Implicit display
      % STACK: [ 6;
                 7;
                10;
                11]
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Classic), 14 bytes

1+⊢⊥¨∘⍳2⍴0⌈-∘2

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Batch, 85 bytes

@for /l %%i in (3,1,%1)do @for /l %%j in (3,1,%1)do @cmd/cset/a(%%i-2)*%1+%%j-1&echo(

I can't easily loop from 2 to m-1 so I loop from 3 to m and adjust in the calculation.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Julia 0.6, 36 bytes

m->reshape(1:m*m,(m,m))[2:m-1,2:m-1]

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Pari/GP, 26 bytes

n->[x|x<-[n..n^2-n],x%n>1]

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Japt, 12 bytes

I spent so long golfing the extraction of elements that I ran out of time to golf the array generation. I'm also only now noticing that we can take n as input instead so I may be able to save something there. To be revisited ...

òUnU²)òU m¤c

Try it


Explanation

                 :Implicit input of integer U=m     :e.g., 4
   U²            :U squared                         :16
 Un              :Minus U                           :12
ò                :Range [U,U**2-U]                  :[4,5,6,7,8,9,10,11,12]
      òU         :Partitions of length U            :[[4,5,6,7],[8,9,10,11],[12]]
         m       :Map
          ¤      :  Remove first 2 elements         :[[6,7],[10,11],[]]
           c     :Flatten                           :[6,7,10,11]
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

J, 23 19 bytes

-4 bytes thanks to FrownyFrog!

1 1}:@}.-@%:}:\1+i.

Try it online!

My original olution:

J, 23 bytes

[:|:@}:@}.^:2-@%:]\1+i.

Takes n as input, returns a matrix

How it works

1+i. - generates a list 1..n

-@%: - finds the square root of n and negates it (m)

]\ - makes a table (matrix) m x m from the list

^:2 - do the following twice:

|:@}:@}. - drop the first row, then drop the last row, then transpose

[: - cap the fork

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 1}:@}.-@%:}.@}:\1+i. \$\endgroup\$ – FrownyFrog Feb 3 '18 at 13:33
  • 1
    \$\begingroup\$ No, 1 1}:@}.-@%:}:\1+i. \$\endgroup\$ – FrownyFrog Feb 3 '18 at 13:40
  • \$\begingroup\$ @FrownyFrog - Cool, thanks! I didn't know about the list left argument of }. \$\endgroup\$ – Galen Ivanov Feb 3 '18 at 17:26
2
\$\begingroup\$

Husk, 9 bytes

‼ȯTthS↑CN

Try it online!

Explanation

‼ȯTthS↑CN  Implicit input, say m=4.
       CN  Cut the natural numbers by m: [[1,2,3,4],[5,6,7,8],[9,10,11,12],..
     S↑    Take first m lists: [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
‼ȯ         Do this twice:
    h       Remove last row,
   t        remove first row,
  T         transpose.
           Result is [[6,7],[10,11]]; print it implicitly.
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Japt, 14 bytes

²õ òU ÅkJ ®ÅkJ

Takes m as input

Explanation

 ²õ òU ÅkJ ®ÅkJ                                      
                // U = input                         | 3
U²              // U squared                         | 9
  õ             // Range [1...U²]                    | [1,2,3,4,5,6,7,8,9]
    òU          // Cut into slices of U              | [[1,2,3],[4,5,6],[7,8,9]]
       Å        // Remove the first item             | [[4,5,6],[7,8,9]]
        kJ      // Remove the last item              | [[4,5,6]]
           ®    // Map:                              |
            ÅkJ //   Remove the first and last items | 5     

Try it online!


The solution that takes n is also 14 bytes:

õ òU¬ ÅkJ ®ÅkJ

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 44 43 bytes (tokenized)

DC 4D 3F CE 4D 6D 32 3F CF 3F DE 2A 08 09 3F D0 3F 4D 71 32 3F 23 4D 70 32 70 58 70 32 B1 58 83 72 11 2B 58 2B 30 2B 72 0D 71 31

Readable version:

:Input M
:If M≤2
:Then
:Disp "{}
:Else
:M-2
:seq(M+2+X+2int(X/Ans),X,0,Ans²-1

It was unfortunately necessary to print empty lists manually since TI-BASIC does not normally allow that. If m were given greater than two, the code could be reduced to just 29 bytes.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Pyth, 10 bytes

mtPdtPcQS*

Try it here!

Takes m as input.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Red, 63 62 bytes

f: func[n][repeat i(n - 2 * n)[if(a: n + i)// n > 1[print a]]]

Try it online!

This is a Red port of totallyhuman's Haskell / Mr. Xcoder's Python 2 solution

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Clean, 45 bytes

import StdEnv
$m=[i\\i<-[m..m*m-m]|i rem m>1]

Try it online!

This is just totallyhuman's Haskell answer but in Clean.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Pyt, 13 bytes

ĐĐ⁻⁻ř*⇹⁻⁻ř⁺ɐ+

Port of Jonathan Allan's Jelly answer

Explanation:

                    Implicit input (takes m)
ĐĐ                  Triplicate the input (push it on the stack two more times)
  ⁻⁻                Decrement top of stack twice
    ř               Push [1,2,...,m-2]
     *              Multiplies by m
      ⇹             Swaps top two items on stack
       ⁻⁻           Decrement (m-2 is now on top)
         ř          Push [1,2,...,m-2]
          ⁺         Increment each element by 1
           ɐ+       Add [2,3,...,m-1] to each element of [m,2m,...,m(m-2)]
                    Implicit print

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Python, 111 bytes

def f(s):
 r=[]
 for i in[i[1:-1]for i in[[(j*s)+i+1 for i in range(s)]for j in range(s)][1:-1]]:r+=i
 return r
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Java 8, 241 183 170 162 160 132 122 bytes

j->{if(j<3)return new int[1];int e[]=new int[j*j-4*j+4],x=0,i=0;for(;++i<=j*j;)if(!(i<j|i>j*j-j|i%j<2))e[x++]=i;return e;}

Try it online!

Java makes it very tough(lots of bytes) when you have to create an array of somewhat "unknown" size.

  • -8 bytes thanks to Magic Octopus Urn
  • -28 bytes thanks to Mr. Xcoder
  • -10 bytes thanks to Kevin Cruijssen
|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ Also, yes, Java is rough for code-golf. But you're obviously good at it. Man, you need to check out this language called Groovy it's basically shorthand Java. \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 22:00
  • 2
    \$\begingroup\$ 132 bytes by removing an extra condition from the if statement, and various tricks. \$\endgroup\$ – Mr. Xcoder Feb 3 '18 at 12:23
  • 1
    \$\begingroup\$ 122 bytes continuing @Mr.Xcoder's 132-byte version above by combining the int, changing the || to |, and removing the brackets of the single-line if-body. \$\endgroup\$ – Kevin Cruijssen Feb 5 '18 at 9:20
  • 1
    \$\begingroup\$ 101 bytes \$\endgroup\$ – ceilingcat Oct 24 '19 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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