Latest update

# Perfect Squares Without Borders

2018-02-02 21:29:55

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 may 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 code-golf, lowest byte-count wins.

C, 50 bytes

i;f(m){for(i=m;++i1&&printf("%d ",i);}

Try it online!

Pure Bash, 49

• C, 50 bytes

i;f(m){for(i=m;++i1&&printf("%d ",i);}

Try it online!

2018-02-02 21:36:32
• Pure Bash, 49

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.

2018-02-02 21:47:15

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

Try it online!

2018-02-02 21:51:08
• Octave, 31 bytes

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

Returns a matrix.

Try it online!

2018-02-02 21:54:56
• Jelly, 8 bytes

sÆ½ḊṖ\$⁺€

Try it online!

2018-02-02 21:55:11
• R, 44 bytes

function(m)matrix(1:m^2,m,m,T)[-m,-m][-1,-1]

Try it online!

Returns a matrix.

Generates a matrix, then removes the last row and column with [-m,-m], then removes the first row and column [-1,-1].

2018-02-02 22:06:16
• 05AB1E, 9 bytes

LItä¦¨ε¦¨

Try it online!

2018-02-02 22:10:03
• 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.

2018-02-02 22:12:34
• Bash + GNU utilities, 35

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

Try it online.

2018-02-02 22:22:59
• Pyth, 10 bytes

mtPdtPcQS*

Try it here!

Takes m as input.

2018-02-02 22:24:00
• 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!

2018-02-02 22:33:30
• Java 8, 241 bytes

j->{if(j<9)return new int[1];int q=(int)Math.sqrt(j);int w=(q-2)*(q-2);int[]e=new int[w];int[][]r=new int[q][q];int t=0;int y=0;for(int i=1;i1&&i1&&p

Try it online!

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

This answer needs to be cleaned up, a lot, but I revisit later tonight.

2018-02-02 22:39:17
• Proton, 28 bytes

k=>filter(u=>1

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.

2018-02-02 22:44:45
• 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

Ṗ - pop m (implicit range) [1,2,3,4]

× - multiply by m [5,10,15,20]

Ṗ - pop m again [1,2,3,4]

Ṗ - pop the last item [[6,7,8,9],[11,12,13,14],[16,17,18,19]]

Ḋ€ - dequeue €ach [[7,8,9],[12,13,14],[17,18,19]]

2018-02-02 22:54:11
• Red, 63 bytes

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

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

Try it online!

2018-02-02 22:57:04