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.

A174344 List of x-coordinates of point moving in clockwise square spiral.

Original entry on oeis.org

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, 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, 2, 1, 0, -1, -2, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -2
Offset: 1

Views

Author

Nikolas Garofil (nikolas(AT)garofil.be), Mar 16 2010

Keywords

Comments

Also, list of x-coordinates of point moving in counterclockwise square spiral.
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
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

Examples

			Here is the beginning of the clockwise square spiral. Sequence gives x-coordinate of the n-th point.
.
  20--21--22--23--24--25
   |                   |
  19   6---7---8---9  26
   |   |           |   |
  18   5   0---1  10  27
   |   |       |   |   |
  17   4---3---2  11  28
   |               |   |
  16--15--14--13--12  29
                       |
  35--34--33--32--32--30
.
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
		

References

  • 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.

Crossrefs

Cf. A180714. A268038 (or A274923) gives sequence of y-coordinates.
The (x,y) coordinates for a point sweeping a quadrant by antidiagonals are (A025581, A002262). - N. J. A. Sloane, Jul 17 2018
See A296030 for the pairs (A174344(n), A274923(n)). - M. F. Hasler, Oct 20 2019
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

Programs

  • Julia
    function SquareSpiral(len)
        x, y, i, j, N, n, c = 0, 0, 0, 0, 0, 0, 0
        for k in 0:len-1
            print("$x, ") # or print("$y, ") for A268038.
            if n == 0
                c += 1; c > 3 && (c =  0)
                c == 0 && (i = 0; j =  1)
                c == 1 && (i = 1; j =  0)
                c == 2 && (i = 0; j = -1)
                c == 3 && (i = -1; j = 0)
                c in [1, 3] && (N += 1)
                n = N
            end
            n -= 1
            x, y = x + i, y + j
    end end
    SquareSpiral(75) # Peter Luschny, May 05 2019
    
  • Maple
    fx:=proc(n) option remember; local k; if n=1 then 0 else
    k:=floor(sqrt(4*(n-2)+1)) mod 4;
    fx(n-1) + sin(k*Pi/2); fi; end;
    [seq(fx(n),n=1..120)]; # Based on Seppo Mustonen's formula. - N. J. A. Sloane, Jul 11 2016
  • Mathematica
    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 *)
  • PARI
    L=0; d=1;
    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
    
  • 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(nKevin Ryde, Sep 16 2019
    
  • 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
    
  • Python
    # Based on Kevin Ryde's PARI script
    import math
    def A174344(n):
        n -= 1
        m = math.isqrt(n)
        k = math.ceil(m/2)
        n -= 4*k*k
        if n < 0: return k if n < -m else -k-n
        return -k if n < m else n-3*k # David Radcliffe, Aug 04 2025

Formula

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
a(n) = A010751(A037458(n-1)) for n>1. - William McCarty, Jul 29 2021

Extensions

Link corrected by Seppo Mustonen, Sep 05 2010
Definition clarified by N. J. A. Sloane, Dec 20 2012