21
Lonely primes (as I call them) are primes, where given a number grid with width w ≥ 3
, are primes which do not have any other primes adjacent to them orthogonally or diagonally.
For example, if we take this grid where w = 12
(primes highlighted in bold):
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23...
...86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120
You can see that only the two primes 103 and 107 have no primes orthogonally or diagonally adjecant whatsoever. I've skipped over a section because there's no lonely primes there. (except 37, actually)
Your task is to, given two inputs w ≥ 3
and i ≥ 1
, determine the first lonely prime in a number grid with width w
, where said lonely prime must be greater than or equal to i
. Inputs may be taken in any reasonable format (including taking them as strings). It is guaranteed there will be a lonely prime for width w
.
The grid doesn't wrap around.
Examples:
w i output
11 5 11
12 104 107
12 157 157
9 1 151
12 12 37
As this is code-golf, shortest code wins!
Why is for
w=12
not37
a lonely prime? None of the numbers surrounding it --{25, 26, 38, 49, 50}
-- are prime. – Jonathan Frech – 2018-01-12T15:46:08.577@JonathanFrech Yes, a test case includes that. – Okx – 2018-01-12T15:53:43.883