cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

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.

This page as a plain text file.
%I A383183 #15 May 20 2025 15:33:00
%S A383183 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,
%T A383183 51,52,28,12,30,54,55,89,131,179,129,87,127,85,84,124,173,229,293,227,
%U A383183 169,223,167,119,80,81,82,122,120,121,168,170,171,123,172,228,292,226,224,225,287,359,439
%N 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.
%C A383183 The infinite 2D grid is labeled along a square spiral as shown in A316328, starting with 0 at the origin (0,0), where the n-th shell contains the 8n points with sup norm n, as follows:
%C A383183 .
%C A383183   16--15--14--13--12   :
%C A383183    |               |   :
%C A383183   17   4---3---2  11  28
%C A383183    |   |       |   |   |
%C A383183   18   5   0---1  10  27
%C A383183    |   |           |   |
%C A383183   19   6---7---8---9  26
%C A383183    |                   |
%C A383183   20--21--22--23--24--25
%C A383183 .
%C A383183 The cursor is moving like a chess king to the von Neumann neighbor not visited earlier and labeled with the smallest prime number if possible, otherwise with the smallest possible composite number.
%C A383183 After the 171th move, the cursor is trapped in the point (3,0) labeled a(171) = 33. All eight neighbors were then already visited earlier, so the king has no more any possible move.
%H A383183 M. F. Hasler, <a href="/A383183/b383183.txt">Table of n, a(n) for n = 0..171</a> (full sequence).
%H A383183 M. F. Hasler, <a href="/A383183/a383183_1.png">Path plot of A383183(0..171)</a>. (The 8 dark blue points around the red ending point represent the 8 already visited neighbors.) May 13, 2025.
%e A383183 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.
%e A383183 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).
%e A383183 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.
%o A383183 (Python)
%o A383183 from sympy import isprime # = A010051
%o A383183 def square_number(z): return int(4*y**2-y-x if (y := z.imag) >= abs(x := z.real)
%o A383183     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)
%o A383183 def A383183(n, moves=(1, 1+1j, 1j, 1j-1, -1, -1-1j, -1j, 1-1j)):
%o A383183     if not hasattr(A:=A383183, 'terms'): A.terms=[0]; A.pos=0; A.path=[0]
%o A383183     while len(A.terms) <= n:
%o A383183         try: _,s,z = min((1-isprime(s), s, z) for d in moves if
%o A383183                      (s := square_number(z := A.pos+d)) not in A.terms)
%o A383183         except ValueError:
%o A383183             raise IndexError(f"Sequence has only {len(A.terms)} terms")
%o A383183         A.terms.append(s); A.pos = z; A.path.append(z)
%o A383183     return A.terms[n]
%o A383183 A383183(999) # gives IndexError: Sequence has only 172 terms
%o A383183 A383183.terms # shows the full sequence; append [:N] to show only N terms
%o A383183 import matplotlib.pyplot as plt # this and following to plot the path:
%o A383183 plt.plot([z.real for z in A383183.path], [z.imag for z in A383183.path])
%o A383183 plt.show()
%Y A383183 Cf. A383184 (the same with "diamond spiral" numbering).
%Y A383183 Cf. A383185 (similar with |a(n)-a(n+1)| > 2 instead of the prime number condition).
%Y A383183 Cf. A335856 (same with the square spiral and indices starting at 1).
%Y A383183 Cf. A316328 (knight path on square spiral numbered board).
%K A383183 nonn,walk,fini,full
%O A383183 0,2
%A A383183 _M. F. Hasler_, May 13 2025