%I #87 Jan 28 2023 10:34:58
%S 0,1,1,0,-1,-1,-1,0,1,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1,0,1,2,3,3,3,3,
%T 3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1,0,1,2,3,4,4,4,4,4,4,4,4,3,
%U 2,1,0,-1,-2,-3,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-2
%N List of x-coordinates of point moving in clockwise square spiral.
%C Also, list of x-coordinates of point moving in counterclockwise square spiral.
%C This spiral, in either direction, is sometimes called the "Ulam spiral", but "square spiral" is a better name. (Ulam looked at the positions of the primes, but of course the spiral itself must be much older.) - _N. J. A. Sloane_, Jul 17 2018
%C Graham, Knuth and Patashnik give an exercise and answer on mapping n to square spiral x,y coordinates, and back x,y to n. They start 0 at the origin and first segment North so their y(n) is a(n+1). In their table of sides, it can be convenient to take n-4*k^2 so the ranges split at -m, 0, m. - _Kevin Ryde_, Sep 16 2019
%D Ronald L. Graham, Donald E. Knuth, Oren Patashnik, Concrete Mathematics, Addison-Wesley, 1989, chapter 3, Integer Functions, exercise 40 page 99 and answer page 498.
%H Peter Kagey, <a href="/A174344/b174344.txt">Table of n, a(n) for n = 1..10000</a>
%H Seppo Mustonen, <a href="http://www.survo.fi/demos/index.html#ex51">Ulam spiral in color</a> [Interactive web page]
%H Seppo Mustonen, <a href="/A174344/a174344.png">Ulam spiral in color</a> [Local copy of a snapshot of the page]
%H Hugo Pfoertner, <a href="http://oeis.org/plot2a?name1=A174344&name2=A274923&tform1=untransformed&tform2=untransformed&shift=0&radiop1=xy&drawlines=true">Visualization of spiral using Plot 2</a>, May 29 2018
%H N. J. A. Sloane, <a href="/A174344/a174344.png">Ulam spiral in color</a>.
%H Aaron Snook, <a href="http://www.cs.cmu.edu/afs/cs/user/mjs/ftp/thesis-program/2012/theses/snook.pdf">Augmented Integer Linear Recurrences</a>, Thesis, 2012.
%H <a href="/index/Con#coordinates_2D_curves">Index entries for sequences related to coordinates of 2D curves</a>
%F a(1) = 0, a(n) = a(n-1) + sin(floor(sqrt(4n-7))*Pi/2). For a corresponding formula for the y-coordinate, replace sin with cos. - _Seppo Mustonen_, Aug 21 2010 with correction by _Peter Kagey_, Jan 24 2016
%F a(n) = A010751(A037458(n-1)) for n>1. - _William McCarty_, Jul 29 2021
%e Here is the beginning of the clockwise square spiral. Sequence gives x-coordinate of the n-th point.
%e .
%e 20--21--22--23--24--25
%e | |
%e 19 6---7---8---9 26
%e | | | |
%e 18 5 0---1 10 27
%e | | | | |
%e 17 4---3---2 11 28
%e | | |
%e 16--15--14--13--12 29
%e |
%e 35--34--33--32--32--30
%e .
%e Given the offset equal to 1, a(n) gives the x-coordinate of the point labeled n-1 in the above drawing. - _M. F. Hasler_, Nov 03 2019
%p fx:=proc(n) option remember; local k; if n=1 then 0 else
%p k:=floor(sqrt(4*(n-2)+1)) mod 4;
%p fx(n-1) + sin(k*Pi/2); fi; end;
%p [seq(fx(n),n=1..120)]; # Based on _Seppo Mustonen_'s formula. - _N. J. A. Sloane_, Jul 11 2016
%t a[n_]:=a[n]=If[n==0,0,a[n-1]+Sin[Mod[Floor[Sqrt[4*(n-1)+1]],4]*Pi/2]]; Table[a[n],{n,0,50}] (* _Seppo Mustonen_, Aug 21 2010 *)
%o (PARI) L=0; d=1;
%o for(r=1,9,d=-d;k=floor(r/2)*d;for(j=1,L++,print1(k,", "));forstep(j=k-d,-floor((r+1)/2)*d+d,-d,print1(j,", "))) \\ _Hugo Pfoertner_, Jul 28 2018
%o (PARI) a(n) = n--; my(m=sqrtint(n),k=ceil(m/2)); n -= 4*k^2; if(n<0, if(n<-m, k, -k-n), if(n<m, -k, n-3*k)); \\ _Kevin Ryde_, Sep 16 2019
%o (PARI) apply( A174344(n)={my(m=sqrtint(n-=1), k=m\/2); if(n < 4*k^2-m, k, 0 > n -= 4*k^2, -k-n, n < m, -k, n-3*k)}, [1..99]) \\ _M. F. Hasler_, Oct 20 2019
%o (Julia)
%o function SquareSpiral(len)
%o x, y, i, j, N, n, c = 0, 0, 0, 0, 0, 0, 0
%o for k in 0:len-1
%o print("$x, ") # or print("$y, ") for A268038.
%o if n == 0
%o c += 1; c > 3 && (c = 0)
%o c == 0 && (i = 0; j = 1)
%o c == 1 && (i = 1; j = 0)
%o c == 2 && (i = 0; j = -1)
%o c == 3 && (i = -1; j = 0)
%o c in [1, 3] && (N += 1)
%o n = N
%o end
%o n -= 1
%o x, y = x + i, y + j
%o end end
%o SquareSpiral(75) # _Peter Luschny_, May 05 2019
%Y Cf. A180714. A268038 (or A274923) gives sequence of y-coordinates.
%Y The (x,y) coordinates for a point sweeping a quadrant by antidiagonals are (A025581, A002262). - _N. J. A. Sloane_, Jul 17 2018
%Y See A296030 for the pairs (A174344(n), A274923(n)). - _M. F. Hasler_, Oct 20 2019
%Y The diagonal rays are: A002939 (2*n*(2*n-1): 0, 2, 12, 30, ...), A016742 = (4n^2: 0, 4, 16, 36, ...), A002943 (2n(2n+1): 0, 6, 20, 42, ...), A033996 = (4n(n+1): 0, 8, 24, 48, ...). - _M. F. Hasler_, Oct 31 2019
%K sign
%O 1,10
%A Nikolas Garofil (nikolas(AT)garofil.be), Mar 16 2010
%E Link corrected by _Seppo Mustonen_, Sep 05 2010
%E Definition clarified by _N. J. A. Sloane_, Dec 20 2012