A383185
Number of the square visited by a king moving on a spirally numbered board always to the lowest available unvisited square, when a wall delimiting the spiral must be crossed on each move.
Original entry on oeis.org
0, 3, 13, 2, 10, 1, 7, 21, 6, 18, 4, 14, 32, 12, 28, 11, 27, 9, 23, 8, 22, 44, 20, 40, 19, 5, 17, 37, 16, 34, 15, 33, 59, 31, 57, 30, 54, 29, 53, 85, 51, 25, 47, 24, 46, 76, 45, 75, 43, 73, 42, 70, 41, 69, 39, 67, 38, 66, 36, 62, 35, 61, 95, 60, 94, 58, 92, 56, 88, 55, 87, 127, 86, 52, 26
Offset: 0
For n = 1, a(1) = 3 because moving from 0 to 1 or 2 does not pass through a wall.
Cf.
A375925 (the same with indices and numbers of squares starting at 1).
-
def square_number(z): return int(4*y**2-y-x if (y := z.imag) >= abs(x := z.real)
else 4*x**2-x-y if -x>=abs(y) else (4*y-3)*y+x if -y>=abs(x) else (4*x-3)*x+y)
def A383185(n):
if not hasattr(A:=A383185, 'terms'): A.terms=[0]; A.pos=0; A.path=[0]
while len(A.terms) <= n:
s,d = min((s,d) for d in (1, 1+1j, 1j, 1j-1, -1, -1-1j, -1j, 1-1j) if
abs((s:=square_number(A.pos+d))-A.terms[-1]) > 2 and s not in A.terms)
A.terms.append(s); A.pos += d; A.path.append(A.pos)
return A.terms[n]
import matplotlib.pyplot as plt # this and below to plot the trajectory
plt.plot([z.real for z in A383185.path], [z.imag for z in A383185.path])
plt.show()
A351043
Lexicographically earliest non-extendable Racetrack trajectory (using von Neumann neighborhood) on spiral on infinite square grid.
Original entry on oeis.org
0, 1, 9, 24, 46, 45, 21, 6, 4, 15, 33, 32, 12, 11, 10, 8, 7, 5, 16, 36, 63, 97, 96, 60, 13, 27, 50, 80, 119, 165, 164, 116, 75, 41, 68, 66, 64, 99, 141, 140, 138, 93, 55, 86, 84, 49, 79, 78, 76, 43, 69, 104, 102, 100, 143, 193, 192, 190, 137, 57, 54, 52, 25
Offset: 0
A380812
Sequence of x-coordinates of the lexicographically earliest (according to the spiral numbering of the square grid; see comments) infinite Racetrack trajectory (using von Neumann neighborhood) on the square grid.
Original entry on oeis.org
0, 1, 2, 2, 1, 0, -1, -1, -1, -1, 0, 1, 2, 2, 2, 1, 0, -1, -2, -3, -3, -2, -1, 0, 1, 3, 4, 4, 4, 3, 2, 1, -1, -3, -4, -4, -4, -4, -3, -2, 0, 2, 4, 5, 5, 4, 3, 2, 0, -2, -4, -5, -5, -5, -5, -4, -3, -1, 1, 3, 4, 4, 3, 2, 1, -1, -3, -5, -6, -6, -5, -3, 0, 3, 6, 8
Offset: 0
In the 144th step, the car moves from (-9,-8) to (-6,-6) (a(144) = A380813(144) = -6). A priori, the next possible positions (ordered by increasing spiral number) are (-3,-3), (-4,-4), (-3,-4), (-2,-4), and (-3,-5). Of these, (-3,-3) has already been visited (after the 103rd step), so the next choice is (-4,-4). From that position, however, the car is forced to move to (-2,-2) (all other alternatives have already been visited), and from (-2,-2) there are no available positions not already visited (so the trajectory in A351043 ends there). The next option (-3,-4) is also a dead end, but from (-2,-4) it is possible to continue forever, so a(145) = -2 and A380813(145) = -4.
A383183
Square spiral numbers of the n-th grid point visited by a king always moving to the unvisited point labeled with the smallest possible prime or else composite number.
Original entry on oeis.org
0, 2, 3, 5, 7, 23, 47, 79, 48, 24, 8, 1, 11, 13, 31, 29, 53, 27, 9, 10, 26, 25, 49, 83, 50, 51, 52, 28, 12, 30, 54, 55, 89, 131, 179, 129, 87, 127, 85, 84, 124, 173, 229, 293, 227, 169, 223, 167, 119, 80, 81, 82, 122, 120, 121, 168, 170, 171, 123, 172, 228, 292, 226, 224, 225, 287, 359, 439
Offset: 0
From the starting point (0,0) labeled a(0) = 0, the king can reach the point (1,1) labeled 2, which is the smallest possible prime number, so a(1) = 2.
Then the king can reach (1,0) labeled 3 which is the next smaller prime number, so a(2) = 3. From there it can go to (-1,0) labeled 5 = a(3), and then to (0,-1) labeled a(4) 7 = a(4). From there, the only available prime number is 23 = a(5) at (1,-2).
The king continues in that south-east direction, before walking in north-east direction and then back and further south-east up to and beyond the point (10,-10). Then it goes back in the opposite north-west direction up to (-8,7) and (-5, 8), before heading to the point (3,0) where it gets stuck.
Cf.
A383184 (the same with "diamond spiral" numbering).
Cf.
A383185 (similar with |a(n)-a(n+1)| > 2 instead of the prime number condition).
Cf.
A335856 (same with the square spiral and indices starting at 1).
Cf.
A316328 (knight path on square spiral numbered board).
-
from sympy import isprime # = A010051
def square_number(z): return int(4*y**2-y-x if (y := z.imag) >= abs(x := z.real)
else 4*x**2-x-y if -x>=abs(y) else (4*y-3)*y+x if -y>=abs(x) else (4*x-3)*x+y)
def A383183(n, moves=(1, 1+1j, 1j, 1j-1, -1, -1-1j, -1j, 1-1j)):
if not hasattr(A:=A383183, 'terms'): A.terms=[0]; A.pos=0; A.path=[0]
while len(A.terms) <= n:
try: _,s,z = min((1-isprime(s), s, z) for d in moves if
(s := square_number(z := A.pos+d)) not in A.terms)
except ValueError:
raise IndexError(f"Sequence has only {len(A.terms)} terms")
A.terms.append(s); A.pos = z; A.path.append(z)
return A.terms[n]
A383183(999) # gives IndexError: Sequence has only 172 terms
A383183.terms # shows the full sequence; append [:N] to show only N terms
import matplotlib.pyplot as plt # this and following to plot the path:
plt.plot([z.real for z in A383183.path], [z.imag for z in A383183.path])
plt.show()
A306308
Table read by rows: the end square loops for a trapped knight moving on an infinitely large 2-dimensional spirally numbered board starting from any square.
Original entry on oeis.org
404, 3328, 2666, 1338, 736, 1535, 2168, 406, 2444, 2945, 2245, 605, 684, 2663, 2312, 3323, 935, 910
Offset: 1
The 3 end square loops are:
1: 404, 3328, 2666
2: 1338, 736, 1535, 2168, 406, 2444, 2945, 2245, 605, 684, 2663, 2312, 3323
3: 935, 910
Starting the knight from the square 1 leads to the first 3-member loop after two iterations: the sequence of end squares is 2084, 404, 3328, 2666, 404, ... . Starting from the square 2 leads to the second (13-member) loop after ten iterations: the sequence is 711, 632, 4350, 3727, 3610, 7382, 2411, 4632, 4311, 1338, ... . The third (2-member) loop is not seen until the knight starts from square 284, the sequence being entered after two iterations via 1168, 935, 910, 935, ... .
- Scott R. Shannon, Square positions for the 3 loops. The red line connects the 3 points of the first loop, the blue line connects the 13 points of the second loop, and the green line connects the 2 points of the third loop. The white point marks the central square with number 1.
- Scott R. Shannon, Starting square to loop mapping. A plot of the first 302500 starting squares mapped via color to the end square loop into which the corresponding knight path eventually falls: red is the first (3-member) loop, blue the second (13-member) loop, green the third (2-member) loop. The white point marks the central square with number 1 for clarity (it actually falls into the red first loop).
- Scott R. Shannon, The knight's path when starting at square 910. Showing path one of the 2-member loop - the green square is the starting square 910, the red square is the end square 935.
- Scott R. Shannon, The knight's path when starting at square 935. Showing path two of the 2-member loop - the green square is the starting square 935, the red square is the end square 910.
- Scott R. Shannon, Stripped down Java code to produce the loop values.
- N. J. A. Sloane and Brady Haran, The Trapped Knight Numberphile video (2019).
Cf.
A306291,
A316884,
A316967,
A316667,
A316328,
A317106,
A317105,
A317416,
A317415,
A317438,
A317437, and
A323469,
A323470,
A323471,
A323472.
A337170
Squares visited by knight moves on a diagonally back and forth numbered board and moving to the lowest available unvisited square at every step.
Original entry on oeis.org
1, 8, 4, 2, 13, 3, 6, 9, 11, 19, 5, 7, 26, 16, 14, 31, 15, 18, 12, 21, 24, 10, 23, 33, 39, 20, 22, 34, 25, 17, 28, 47, 43, 29, 27, 32, 40, 35, 37, 53, 57, 36, 58, 52, 38, 55, 80, 76, 56, 54, 59, 51, 42, 30, 45, 48, 62, 70, 44, 46, 64, 49, 41, 72, 60, 50, 63
Offset: 1
-
ClearAll[ShowRoute,MakeMove,FindSequence]
knightjump=Select[Tuples[Range[-2,2],2],Norm[#]==Sqrt[5]&];
ShowRoute[output_Association]:=Module[{colors},colors=(ColorData["Rainbow"]/@Subdivide[Length[output["Coordinates"]]-1.0]);
Graphics[{Line[output["Coordinates"],VertexColors->colors],Disk[Last@output["Coordinates"],0.2]}]]
MakeMove[spiral_Association,visited_List]:=Module[{poss,hj},poss=Table[Last[Last[visited]]+hj,{hj,knightjump}];
poss=DeleteMissing[{spiral[#],#}&/@poss,1,\[Infinity]];
poss=Select[poss,FreeQ[visited[[All,2]],Last[#]]&];
If[Length[poss]>0,First[TakeSmallestBy[poss,First,1]],Missing[]]]
FindSequence[start_:{0,0},grid_]:=Module[{positions,j,next},positions={{grid[start],start}};
PrintTemporary[Dynamic[j]];
Do[next=MakeMove[grid,positions];
If[next=!=Missing[],AppendTo[positions,next],Break[];],{j,\[Infinity]}];
<|"Coordinates"->positions[[All,2]],"Indices"->positions[[All,1]]|>]
grid=ResourceFunction["LatticePointsArrangement"]["DiagonalZigZagEastQ4",10000];
grid=Association[MapIndexed[#1->#2[[1]]&,grid]];
ShowRoute[fs=FindSequence[{0,0},grid]]
fs
fs["Indices"]
ListPlot[fs["Indices"]]
A375925
Squares visited by a king moving on a walled, spirally numbered board, where a wall must be jumped on each move, always to the lowest available unvisited square.
Original entry on oeis.org
1, 4, 14, 3, 11, 2, 8, 22, 7, 19, 5, 15, 33, 13, 29, 12, 28, 10, 24, 9, 23, 45, 21, 41, 20, 6, 18, 38, 17, 35, 16, 34, 60, 32, 58, 31, 55, 30, 54, 86, 52, 26, 48, 25, 47, 77, 46, 76, 44, 74, 43, 71, 42, 70, 40, 68, 39, 67, 37, 63, 36, 62, 96, 61, 95, 59, 93
Offset: 1
For n = 2, a(2) = 4 because moving to 2 or 3 does not pass through a wall.
-
def square_number(z): return int(4*y**2-y-x if (y := z.imag) >= abs(x := z.real)
else 4*x**2-x-y if -x>=abs(y) else (4*y-3)*y+x if -y>=abs(x) else (4*x-3)*x+y)
def A375925(n):
if not hasattr(A:=A375925, 'terms'): A.terms=[1]; A.pos=0
while len(A.terms) < n:
s,d = min((s,d) for d in (1, 1+1j, 1j, 1j-1, -1, -1-1j, -1j, 1-1j) if
abs((s:=1+square_number(A.pos+d))-A.terms[-1]) > 2 and s not in A.terms)
A.terms.append(s); A.pos += d
return A.terms[n-1] # M. F. Hasler, May 07 2025
A383190
a(2n) and a(2n+1) are the square spiral numbers of the position on which the (n+1)th domino is placed, when tiling the plane by placing the dominos always as near as possible to the origin and so that no two dominos share a long side. Inverse permutation of A383191.
Original entry on oeis.org
0, 1, 3, 4, 5, 6, 7, 22, 2, 11, 8, 9, 10, 27, 14, 13, 18, 17, 15, 16, 19, 20, 21, 44, 23, 46, 12, 29, 24, 25, 33, 34, 39, 40, 45, 76, 28, 53, 32, 31, 38, 37, 26, 51, 35, 36, 41, 42, 43, 74, 47, 78, 52, 85, 60, 59, 68, 67, 61, 62, 69, 70, 75, 114, 77, 116, 30, 55, 48, 49, 54, 87, 58, 57, 66, 65
Offset: 0
The first domino (0, 1) is placed on the origin and the square to its right.
The second domino (2, 3) is placed above the origin (nearest free point coming first counter-clockwise), oriented to the left (since going to the right would make it side-by-side with the first domino, which is forbidden).
Then the third domino (4, 5) is placed left to the origin, going downwards (using the free point closest to the origin).
The fourth domino (6, 7) is placed below the origin, going downwards (since going to the right would make it parallel to the first domino).
Then the free points nearest to the origin are to its lower right and to its upper right. The upper right is preferred since it comes first counter-clockwise (starting to the right). The domino (8, 9) is placed sidewards, again because the other end upwards) would come later in counter-clockwise sense.
Then the domino (10, 11) is placed to the lower right, also sideways because vertically it would be side-by-side with (6, 7).
The first 16 dominoes are placed as follows, where the numbers (2n-2, 2n-1) represent the two ends of the n-th domino, and t
|
| 19==18 14==15 26==27
|
| 17 3===2 8===9 The sequence is obtained by listing the square
| || spiral number corresponding to the position of
| 16 4 0===1 12==13 the numbers 0, 1, 2, 3...: 0 for 0, 1 for 1,
| ‖ 3 (square spiral number for position (x=0, y=1)
| 20 5 6 10==11 where 2 is placed, 4 for the position of 3,
| || ‖ 5, 6, 7 for the position of 4, 5, 6; then
| 21 22 7 24 28==29 22 for the position (x=0, y=-2) of 7, 2 for 8, ...
| || ||
| 23 25
|
We see that all dominoes in the upper right half plane are placed horizontally, and in the lower left half they are placed vertically.
Cf.
A383191 (inverse permutation),
A316328 (knight tour with illustration of the square spiral),
A174344 (square spiral, but clockwise).
-
class A383190: # use A383190(n) or a=A383190(); a[n]; print(a); for x in a: ...
border, grid, terms = {0}, {}, []
neighbors = (1, 1+1j, 1j, 1j-1, -1, -1-1j, -1j, 1-1j)
def _str_(self): #"ASCII representation of (filled part of) the plane."
X = sorted({z.real for z in self.grid}); L = len(str(len(self.terms)))+1
return "\n".join("".join(f"{self.grid.get(x+y*1j,'')!s:{L}}" for x in X)
for y in sorted({z.imag for z in self.grid}, reverse=1))
def _new_(cls, n: int | None = None) -> int | object:
"""Return a(n) or the sequence object if no n is given."""
return super()._new_(cls) if n is None else super()._new_(cls)(n)
def _call_(self, n: int | None = None) -> int | object:
"""Return a(n) or the sequence object if no n is given."""
return self if n is None else self.extend(upto=n) or self.terms[n]
def _getitem_(self, n: int | slice) -> int | list:
return self(n) if isinstance(n, int) else [self(n) for n in
range(*n.indices(max(len(self.terms), abs(n.stop or 0))))]
def extend(self, upto=None):
"Place new domino(s) on the grid, as 'close' as possible to the origin."
while len(self.terms) <= (upto or (upto := len(self.terms))):
self.fill(pos := min(self.border, key = self.distance))
self.fill(self.second(pos))
def distance(self, pos):
"Return (abs(pos), arg(pos)), where 0 <= arg < τ = 2π."
return abs(pos), atan2(pos.imag, pos.real) % tau
def fill(self, pos):
"""Fill the next available number into grid[pos], increment, and
update border = {free cells that are neighbor to a filled cell}."""
self.grid[pos] = len(self.terms); self.border.remove(pos)
self.border |= {pos+N for N in self.neighbors if pos+N not in self.grid}
self.terms += [int(4*y**2-y-x if (y:=pos.imag)>=abs(x:=pos.real) else
4*x**2-x-y if -x>=abs(y) else (4*y-3)*y+x if -y>=abs(x) else(4*x-3)*x+y)]
def second(self,pos):
"""Find the second cell adjacent to 'pos' so that the domino is oriented
according the rules (closest to origin but no domino side-by-side)."""
return min((pos+dir for dir in(1, 1j, -1, -1j) if pos+dir not in self.grid
and all(self.grid.get(p+dir, -1) != self.grid.get(p, -1) ^ 1
for p in (pos+1j*dir, pos-1j*dir) ) ), key = self.distance)
from math import atan2, tau
print(A383190()[:50])
print(A383190()) # displays the grid filled so far
A383191
a(n) is the number on the n-th position on the square spiral on the plane tiled with dominoes always placed nearest to the origin and so that no two dominos share a long side. Inverse permutation of A383190.
Original entry on oeis.org
0, 1, 8, 2, 3, 4, 5, 6, 10, 11, 12, 9, 26, 15, 14, 18, 19, 17, 16, 20, 21, 22, 7, 24, 28, 29, 42, 13, 36, 27, 66, 39, 38, 30, 31, 44, 45, 41, 40, 32, 33, 46, 47, 48, 23, 34, 25, 50, 68, 69, 76, 43, 52, 37, 70, 67, 108, 73, 72, 55, 54, 58, 59, 80, 81, 75, 74, 57, 56, 60, 61, 84, 85, 86, 49, 62
Offset: 0
The first domino (0, 1) is placed on the origin and the square to its right.
The second domino (2, 3) is placed above the origin (nearest free point coming first counter-clockwise), oriented to the left (since going to the right would place it side-by-side with the first domino, which is forbidden).
Then the third domino (4, 5) is placed left to the origin, going downwards (using the free point closest to the origin).
The fourth domino (6, 7) is placed below the origin, going downwards (since going to the right would make it parallel to the first domino).
Then the free points nearest to the origin are to its lower right and to its upper right. The upper right is preferred since it comes first counter-clockwise (starting to the right). The domino (8, 9) is placed sidewards, again because the other end upwards) would come later in counter-clockwise sense.
Then the domino (10, 11) is placed to the lower right, also sideways because vertically it would be side-by-side with (6, 7).
The first 16 dominoes are placed as follows, where the numbers (2n-2, 2n-1) represent the two ends of the n-th domino:
|
| 19==18 14==15 26==27
|
| 17 3===2 8===9
| ||
| 16 4 0===1 12==13 The sequence is obtained by reading the numbers
| ‖ along the square spiral: starting at the origin
| 20 5 6 10==11 (0), go 1 right (1), 1 up (8), 2 left (2 and 3),
| || ‖ 2 down (4 and 5), 3 right (6, 10, 11), 3 up
| 21 22 7 24 28==29 (12, 9, 26) 4 left (15, 14, 18, 19), etc.
| || ||
| 23 25
|
We see that all dominoes in the upper right half plane are placed horizontally, and in the lower left half they are placed "vertically".
Cf.
A383190 (inverse permutation),
A316328 (knight tour with illustration of square spiral),
A174344 (square spiral, but clockwise),
A316667 (square spiral starting with 1).
-
class A383191(A383190): # use A383191(n) or a=A383191(); a(n); a[m:n]; print(a)
def _call_(self, n: int) -> int:
"Return A383191(n)."
if isinstance(n, int) and n >= 0:
while n not in self.terms: self.extend()
return self.terms.index(n) # this is the inverse permutation of 'terms'
if n is None: return self # because A383191() => A383190(n=None) => a(n)
raise ValueError(f"Expected non-negative index n, got {n = !r}.")
A383191()[:50] # list [a(0) .. a(49)]
print(A383191()) # displays the grid filled so far (same as A383190)
A306421
End squares for a trapped knight moving on a spirally numbered 2D grid where each square can be visited n times.
Original entry on oeis.org
2084, 124561, 1756923, 21375782, 48176535, 128322490, 196727321, 230310289, 606217402, 2856313870, 244655558, 659075420, 586292888, 1646774611, 1018215514, 719687377, 564513339, 2779028614, 298995630, 1641747842, 414061107, 1467655587, 584309414, 1584716050
Offset: 1
For n = 1, the knight becomes trapped at square 2084 (see A316667). The following table gives the corresponding values for n = 1 through 35:
.
| Square at which | Number of steps
| the knight is | before the
n | trapped | knight is trapped
---+-----------------+--------------
1 | 2084 | 2016 (A316667)
2 | 124561 | 244273
3 | 1756923 | 4737265
4 | 21375782 | 98374180
5 | 48176535 | 258063291
6 | 128322490 | 836943142
7 | 196727321 | 1531051657
8 | 230310289 | 1897092533
9 | 606217402 | 5253106114
10 | 2856313870 | 27296872250
11 | 244655558 | 2772304666
12 | 659075420 | 8437814958
13 | 586292888 | 7875951360
14 | 1646774611 | 24511621133
15 | 1018215514 | 15493169264
16 | 719687377 | 11643899003
17 | 564513339 | 9593491769
18 | 2779028614 | 49835086546
19 | 298995630 | 5734502340
20 | 1641747842 | 33370972720
21 | 414061107 | 8844741817
22 | 1467655587 | 32843399937
23 | 584309414 | 13583967470
24 | 1584716050 | 37945957450
25 | 2544445470 | 62083869640
26 | 4796115990 | 125967045044
27 | 1881606731 | 51291895045
28 | 1321212795 | 37635024035
29 | 6693611092 | 196994700434
30 | 687619472 | 19985943874
31 | 1495794139 | 45392651369
32 | 6677258413 | 213836002227
33 | 6451059544 | 219770103702
34 | 7958333435 | 277128625469
35 | 13924943879 | 485324576539
- Scott R. Shannon, Simplified Java code for producing the series
- Scott R. Shannon, Visited positions for n=3. For clarity only the visited positions are shown. Blue=3 visits, Green=2 visits, White=1 visit. Red is the final square (near top right corner). Note that the internal positions are all visited the maximum 3 times, and that the overall shape becomes an 'indented square' -- this pattern becomes more pronounced as n increases. Likewise the maximum visited x and y distances relative to the central square approach equality as n increases e.g. for n=35 both the maximum x and y visited distances are 59855.
- N. J. A. Sloane and Brady Haran, The Trapped Knight, Numberphile video (2019)
Comments