# 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. – DevelopingDeveloper 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. – Magic Octopus Urn Feb 2 '18 at 21:51
• If outputting as a 2D array, can a single empty array be included in the result? – Shaggy Feb 3 '18 at 9:41

# C, 50 bytes

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


Try it online!

# Octave, 31 bytes

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


Returns a matrix.

Try it online!

• Nice! I've never come across the vec2mat function before. – Tom Carpenter Feb 3 '18 at 18:05

# 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


# 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]
Ḋ€ - dequeue €ach                   [[7,8,9],[12,13,14],[17,18,19]]

• Didn't feel like doing the python one ;)? – Magic Octopus Urn Feb 2 '18 at 21:51

# Pure Bash, 49

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


Or the interesting answer for 52:

(($1>2))&&eval echo \$[$1*{1..$[$1-2]}+{2..$[$1-1]}]  # 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 • Wait, this isn't valid, f(5) should be 7,8,9,12,13,14,17,18,19 – Magic Octopus Urn Feb 2 '18 at 19:49 • Wait, oops I'm a dumbass. – totallyhuman Feb 2 '18 at 19:54 # R, 4443 32 bytes function(n)(x=n:(n^2-n))[x%%n>1]  Try it online! Returns a vector. • Neat output format, is that by default how a matrix is output in R? – Magic Octopus Urn Feb 2 '18 at 19:51 • Yep, that's the print function for a matrix. – Giuseppe Feb 2 '18 at 19:51 • You can omit the second m in matrix(1:m^2,m,m,T): matrix(1:m^2,m,,T) – JAD Feb 3 '18 at 13:26 • @JAD yes, of course. Thanks. – Giuseppe Feb 5 '18 at 16:26 • Nice one, what do you think about using scan()? You might save 2 bytes. Try it online! – Robert Hacken Feb 10 '18 at 12:40 # Jelly, 8 bytes sÆ½ḊṖ$⁺€


Try it online!

• Using m one could do ²s⁸ḊṖ$⁺€ too. (I have also posted another m alternative method.) – Jonathan Allan Feb 2 '18 at 21:14 • @JonathanAllan Already discovered that, but no byte saving over there, you can't remove the ⁸ :( – Erik the Outgolfer Feb 2 '18 at 21:18 # 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. # Wolfram Language (Mathematica), 31 bytes Table[# i+j+1,{i,#-2},{j,#-2}]&  Try it online! # Bash + GNU utilities, 35 seq$1 $[$1*$1-$1]|sed 1~$1d\;2~$1d


# 05AB1E, 9 bytes

LItä¦¨ε¦¨


Try it online!

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

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

# Ruby, 32 bytes

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


Takes m, returns a one-dimensional array.

Try it online!

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



# APL (Dyalog Classic), 14 bytes

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


Try it online!

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

# Julia 0.6, 36 bytes

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


Try it online!

# Pari/GP, 26 bytes

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


Try it online!

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


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

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

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


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

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


: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.

# Pyth, 10 bytes

mtPdtPcQS*


Try it here!

Takes m as input.

# 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

# Clean, 45 bytes

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


Try it online!

# Pyt, 13 bytes

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


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!

# 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


# Java 8, 241183170162160132 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
• 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. – Magic Octopus Urn Feb 2 '18 at 22:00
• 132 bytes by removing an extra condition from the if statement, and various tricks. – Mr. Xcoder Feb 3 '18 at 12:23
• 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. – Kevin Cruijssen Feb 5 '18 at 9:20
• 101 bytes – ceilingcat Oct 24 '19 at 2:19