# Perfect Squares Without Borders

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.
• Complete fault on my end, I read it incorrectly. Feb 2 '18 at 21:46
• @DevelopingDeveloper hey man, if I had a nickle for every time I did that I'd be able to buy a beer or two. Feb 2 '18 at 21:51
• If outputting as a 2D array, can a single empty array be included in the result? Feb 3 '18 at 9:41

# Javascript (63 55 bytes)

m=>[...Array((m-2)*m)].map((x,i)=>i+m).filter(x=>x%m>1)


## Explanation

m =>                    // function signature is m
[...Array((m-2)*m)] // create an array of size m^2 - 2m,
// m^2 is the full array size, taking out 2m removes the top and bottom edges
// filled with undefined
.map((x,i)=>i+m)    // fill with values m to n-m
// (skips the top and bottom edges of the square)
.filter(x=>x%m>1)   // filter out x%m < 2, the left and right sides of the square

• You only use n once, so you can just use m*m directly. Also, the JavaScript (ES6) tips page has a tip for getting rid of fill.
– Neil
Feb 3 '18 at 2:36
• You're right n=m*m is a holdover from a previous iteration when I had multiple references to n. And thanks for the tip on .fill, I figured there was a way to golf it, but I hadn't considered [...Array(x)]. Feb 5 '18 at 16:25

## JavaScript, 47 bytes

  m=>{for(i=m;++i<m*m-m;)if(i%m>1)console.log(i)} // here's the 47-byte script

Also, 49 bytes:

  m=>{for(i=m;(i%m>1&&console.log(i))|++i<m*m-m;);} // here's the 49-byte script

• Your solution presumes the input is assigned to a predefined variable, which we don't permit. Feb 3 '18 at 11:08
• Not only that, but this solution isn't a function or a full program and relies on other variables and values. For this to be considered valid, either rewrite this as a function or a full program (asking for input with prompt()). Here's a sub-optimal 55 bytes long solution for you: (m,a=[])=>{for(i=m;++i<m*m-m;)if(i%m>1)a[i]=i;return a} Feb 3 '18 at 11:46
• Fixed in accordance with rules. Feb 4 '18 at 15:54
• You can golf it further by switching to an arrow function: m=>{for(i=m;++i<m*m-m;)if(i%m>1)console.log(i)}, 47 bytes. Feb 5 '18 at 17:17
• alert is shorter than console.log. Welcome to PPCG, by the way. Feb 6 '18 at 8:54

# APL, 28 Bytes

Will run given value for m

1 1↓¯1 ¯1↓m m⍴⍳m*2


Try it...

### Explanation

m*2 squares m

⍳m*2 gets list from 1 to m*2

m m⍴⍳m*2 wraps list to m by m matrix

¯1 ¯1↓m m⍴⍳m*2 drops -last row an column

1 1↓¯1 ¯1↓m m⍴⍳m*2 drops first row and column

• Welcome to the site! Could you provide some way to test the submission, such as a link to an online interpreter so that others can tell that it is a working solution? Feb 7 '18 at 14:05

# Perl, 30 29 bytes

perl -E 'say\$_+=@a+2for(@a=2..~-<>)x@a' <<< 4