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.
%I A174344 #92 Aug 06 2025 01:01:44 %S A174344 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 A174344 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 A174344 2,1,0,-1,-2,-3,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-2 %N A174344 List of x-coordinates of point moving in clockwise square spiral. %C A174344 Also, list of x-coordinates of point moving in counterclockwise square spiral. %C A174344 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 A174344 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 A174344 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 A174344 Peter Kagey, <a href="/A174344/b174344.txt">Table of n, a(n) for n = 1..10000</a> %H A174344 Seppo Mustonen, <a href="https://www.survo.fi/demos/index.html#ex51">Ulam spiral in color</a> [Interactive web page] %H A174344 Seppo Mustonen, <a href="/A174344/a174344.png">Ulam spiral in color</a> [Local copy of a snapshot of the page] %H A174344 Hugo Pfoertner, <a href="/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 A174344 N. J. A. Sloane, <a href="/A174344/a174344.png">Ulam spiral in color</a>. %H A174344 Aaron Snook, <a href="https://www.cs.cmu.edu/afs/cs/user/mjs/ftp/thesis-program/2012/theses/snook.pdf">Augmented Integer Linear Recurrences</a>, Thesis, 2012. %H A174344 <a href="/index/Con#coordinates_2D_curves">Index entries for sequences related to coordinates of 2D curves</a> %F A174344 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 A174344 a(n) = A010751(A037458(n-1)) for n>1. - _William McCarty_, Jul 29 2021 %e A174344 Here is the beginning of the clockwise square spiral. Sequence gives x-coordinate of the n-th point. %e A174344 . %e A174344 20--21--22--23--24--25 %e A174344 | | %e A174344 19 6---7---8---9 26 %e A174344 | | | | %e A174344 18 5 0---1 10 27 %e A174344 | | | | | %e A174344 17 4---3---2 11 28 %e A174344 | | | %e A174344 16--15--14--13--12 29 %e A174344 | %e A174344 35--34--33--32--32--30 %e A174344 . %e A174344 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 A174344 fx:=proc(n) option remember; local k; if n=1 then 0 else %p A174344 k:=floor(sqrt(4*(n-2)+1)) mod 4; %p A174344 fx(n-1) + sin(k*Pi/2); fi; end; %p A174344 [seq(fx(n),n=1..120)]; # Based on _Seppo Mustonen_'s formula. - _N. J. A. Sloane_, Jul 11 2016 %t A174344 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 A174344 (PARI) L=0; d=1; %o A174344 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 A174344 (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 A174344 (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 A174344 (Julia) %o A174344 function SquareSpiral(len) %o A174344 x, y, i, j, N, n, c = 0, 0, 0, 0, 0, 0, 0 %o A174344 for k in 0:len-1 %o A174344 print("$x, ") # or print("$y, ") for A268038. %o A174344 if n == 0 %o A174344 c += 1; c > 3 && (c = 0) %o A174344 c == 0 && (i = 0; j = 1) %o A174344 c == 1 && (i = 1; j = 0) %o A174344 c == 2 && (i = 0; j = -1) %o A174344 c == 3 && (i = -1; j = 0) %o A174344 c in [1, 3] && (N += 1) %o A174344 n = N %o A174344 end %o A174344 n -= 1 %o A174344 x, y = x + i, y + j %o A174344 end end %o A174344 SquareSpiral(75) # _Peter Luschny_, May 05 2019 %o A174344 (Python) # Based on Kevin Ryde's PARI script %o A174344 import math %o A174344 def A174344(n): %o A174344 n -= 1 %o A174344 m = math.isqrt(n) %o A174344 k = math.ceil(m/2) %o A174344 n -= 4*k*k %o A174344 if n < 0: return k if n < -m else -k-n %o A174344 return -k if n < m else n-3*k # _David Radcliffe_, Aug 04 2025 %Y A174344 Cf. A180714. A268038 (or A274923) gives sequence of y-coordinates. %Y A174344 The (x,y) coordinates for a point sweeping a quadrant by antidiagonals are (A025581, A002262). - _N. J. A. Sloane_, Jul 17 2018 %Y A174344 See A296030 for the pairs (A174344(n), A274923(n)). - _M. F. Hasler_, Oct 20 2019 %Y A174344 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 A174344 sign %O A174344 1,10 %A A174344 Nikolas Garofil (nikolas(AT)garofil.be), Mar 16 2010 %E A174344 Link corrected by _Seppo Mustonen_, Sep 05 2010 %E A174344 Definition clarified by _N. J. A. Sloane_, Dec 20 2012