A296030 Pairs of coordinates for successive integers in the square spiral (counterclockwise).
0, 0, 1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1, 2, -1, 2, 0, 2, 1, 2, 2, 1, 2, 0, 2, -1, 2, -2, 2, -2, 1, -2, 0, -2, -1, -2, -2, -1, -2, 0, -2, 1, -2, 2, -2, 3, -2, 3, -1, 3, 0, 3, 1, 3, 2, 3, 3, 2, 3, 1, 3, 0, 3, -1, 3, -2, 3, -3, 3, -3, 2
Offset: 1
Examples
The integer 1 occupies the initial position, so its coordinates are {0,0}; therefore a(1)=0 and a(2)=0. The integer 2 occupies the position immediately to the right of 1, so its coordinates are {1,0}. The integer 3 occupies the position immediately above 2, so its coordinates are {1,1}; etc.
References
- S. Wolfram, A New Kind of Science, Wolfram Media, 2002; p. 935.
Links
- Benjamin Mintz, Table of n, a(n) for n = 1..100000
- BackIssues.com, Scientific American March 1964 back issue
- Scientific American, March 1964 cover
- Wikipedia, Ulam Spiral.
Crossrefs
Programs
-
Mathematica
f[n_] := Block[{k = Ceiling[(Sqrt[n] - 1)/2], m, t}, t = 2k +1; m = t^2; t--; If[n >= m - t, {k -(m - n), -k}, m -= t; If[n >= m - t, {-k, -k +(m - n)}, m -= t; If[n >= m - t, {-k +(m - n), k}, {k, k -(m - n - t)}]]]]; Array[f, 40] // Flatten (* Robert G. Wilson v, Dec 04 2017 *) f[n_] := Block[{k = Mod[ Floor[ Sqrt[4 If[OddQ@ n, (n + 1)/2 - 2, (n/2 - 2)] + 1]], 4]}, f[n - 2] + If[OddQ@ n, Sin[k*Pi/2], -Cos[k*Pi/2]]]; f[1] = f[2] = 0; Array[f, 90] (* Robert G. Wilson v, Dec 14 2017 *) f[n_] := With[{t = Round@ Sqrt@ n}, 1/2*(-1)^t*({1, -1}(Abs[t^2 - n] - t) + t^2 - n - Mod[t, 2])]; Table[f@ n, {n, 0, 95}] // Flatten (* Mikk Heidemaa May 23 2020, after Stephen Wolfram *)
-
PARI
apply( {coords(n)=my(m=sqrtint(n), k=m\/2); if(m <= n -= 4*k^2, [n-3*k,-k], n >= 0, [-k,k-n], n >= -m, [-k-n,k], [k,3*k+n])}, [0..99]) \\ Use concat(%) to remove brackets '[', ']'. This function gives the coordinates of n on the spiral starting with 0 at (0,0), as shown in Examples for A174344, A274923, ..., so (a(2n-1),a(2n)) = coords(n-1). To start with 1 at (0,0), change n to n-=1 in sqrtint(). The inverse function is pos(x,y) given e.g. in A316328. - M. F. Hasler, Oct 20 2019
-
Python
from math import ceil, sqrt def get_coordinate(n): k=ceil((sqrt(n)-1)/2) t=2*k+1 m=t**2 t=t-1 if n >= m - t: return k - (m-n), -k else: m -= t if n >= m - t: return -k, -k+(m-n) else: m -= t if n >= m-t: return -k+(m-n), k else: return k, k-(m-n-t)
Formula
a(2*n-1) = A174344(n).
abs(a(n+2) - a(n)) < 2.
a(2*n-1)+a(2*n) = A180714(n).
f(n) = floor(-n/4)*ceiling(-3*n/4 - 1/4) mod 2 + ceiling(n/8) (gives the pairs of coordinates for integers in the diagonal rays). - Mikk Heidemaa, May 07 2020
Comments