Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Whose neighbours are hostile?

Introduction

For the purposes of this challenge, we will define the neighbours of an element \$E\$ in a square matrix \$A\$ (such that \$E=A_{i,j}\$) as all the entries of \$A\$ that are immediately adjacent diagonally, horizontally or vertically to \$E\$ (i.e. they "surround" \$E\,ドル without wrapping around).

For pedants, a formal definition of the neighbours of \$A_{i,\:j}\$ for an \$n\times n\$ matix \$A\$ is (0-indexed): $$N_{i,\:j}=\{A_{a,\:b}\mid(a,b)\in E_{i,\:j}\:\cap\:([0,\:n)\:\cap\:\Bbb{Z})^2\}$$ where $$E_{i,\:j}=\{i-1,\:i,\:i+1\}\times \{j-1,\:j,\:j+1\} \text{ \\ } \{i,\:j\}$$

Let's say that the element at index \$i,\:j\$ lives in hostility if it is coprime to all its neighbours (that is, \$\gcd(A_{i,\:j},\:n)=1\:\forall\:n\in N_{i,\:j}\$). Sadly, this poor entry can't borrow even a cup of sugar from its rude nearby residents...

Task

Enough stories: Given a square matrix \$M\$ of positive integers, output one of the following:

  • A flat list of elements (deduplicated or not) indicating all entries that occupy some indices \$i,j\$ in \$M\$ such that the neighbours \$N_{i,\:j}\$ are hostile.
  • A boolean matrix with \1ドル\$s at positions where the neighbours are hostile and \0ドル\$ otherwise (you can choose any other consistent values in place of \0ドル\$ and \1ドル\$).
  • The list of pairs of indices \$i,\:j\$ that represent hostile neighbourhoods.

Reference Implementation in Physica – supports Python syntax as well for I/O. You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. This is code-golf, so the shortest code in bytes (in every language) wins!

Moreover, you can take the matrix size as input too and additionally can take the matrix as a flat list since it will always be square.

Example

Consider the following matrix:

$$\left(\begin{matrix} 64 & 10 & 14 \\ 27 & 22 & 32 \\ 53 & 58 & 36 \\ \end{matrix}\right)$$

The corresponding neighbours of each element are:

i j – E -> Neighbours | All coprime to E?
 |
0 0 – 64 -> {10; 27; 22} | False
0 1 – 10 -> {64; 14; 27; 22; 32} | False
0 2 – 14 -> {10; 22; 32} | False
1 0 – 27 -> {64; 10; 22; 53; 58} | True
1 1 – 22 -> {64; 10; 14; 27; 32; 53; 58; 36} | False
1 2 – 32 -> {10; 14; 22; 58; 36} | False
2 0 – 53 -> {27; 22; 58} | True
2 1 – 58 -> {27; 22; 32; 53; 36} | False
2 2 – 36 -> {22; 32; 58} | False

And thus the output must be one of the following:

  • {27; 53}
  • {{0; 0; 0}; {1; 0; 0}; {1; 0; 0}}
  • {(1; 0); (2; 0)}

Test cases

Input –> Version 1 | Version 2 | Version 3
[[36, 94], [24, 69]] ->
 []
 [[0, 0], [0, 0]]
 []
[[38, 77, 11], [17, 51, 32], [66, 78, 19]] –>
 [38, 19]
 [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
 [(0, 0), (2, 2)]
[[64, 10, 14], [27, 22, 32], [53, 58, 36]] ->
 [27, 53]
 [[0, 0, 0], [1, 0, 0], [1, 0, 0]]
 [(1, 0), (2, 0)]
[[9, 9, 9], [9, 3, 9], [9, 9, 9]] ->
 []
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
 []
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] ->
 [1, 1, 1, 1, 1, 1, 1, 1, 1] or [1]
 [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
 [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
[[35, 85, 30, 71], [10, 54, 55, 73], [80, 78, 47, 2], [33, 68, 62, 29]] ->
 [71, 73, 47, 29]
 [[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]
 [(0, 3), (1, 3), (2, 2), (3, 3)]

Answer*

Draft saved
Draft discarded
Cancel

AltStyle によって変換されたページ (->オリジナル) /