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.

A215468 Sum of the 8 nearest neighbors of n in a rotated-square spiral with positive integers.

Original entry on oeis.org

50, 62, 72, 86, 76, 84, 122, 88, 144, 104, 166, 120, 152, 160, 144, 218, 160, 168, 248, 184, 192, 278, 208, 216, 260, 268, 240, 248, 346, 264, 272, 280, 384, 296, 304, 312, 422, 328, 336, 344, 400, 408, 368, 376, 384, 506, 400, 408, 416, 424, 552, 440, 448, 456, 464, 598
Offset: 1

Views

Author

Alex Ratushnyak, Aug 11 2012

Keywords

Examples

			Spiral begins:
                     85
                     /
                    /
                  84 61-62
                  /  /    \
                 /  /      \
               83 60 41-42 63
               /  /  /    \  \
              /  /  /      \  \
            82 59 40 25-26 43 64
            /  /  /  /    \  \  \
           /  /  /  /      \  \  \
         81 58 39 24 13-14 27 44 65
         /  /  /  /  /    \  \  \  \
        /  /  /  /  /      \  \  \  \
      80 57 38 23 12  5--6 15 28 45 66
      /  /  /  /  /  /    \  \  \  \  \
     /  /  /  /  /  /      \  \  \  \  \
   79 56 37 22 11  4  1--2  7 16 29 46 67
     \  \  \  \  \  \   /  /  /  /  /  /
      \  \  \  \  \  \ /  /  /  /  /  /
      78 55 36 21 10  3  8 17 30 47 68
        \  \  \  \  \   /  /  /  /  /
         \  \  \  \  \ /  /  /  /  /
         77 54 35 20  9 18 31 48 69
           \  \  \  \   /  /  /  /
            \  \  \  \ /  /  /  /
            76 53 34 19 32 49 70
              \  \  \   /  /  /
               \  \  \ /  /  /
               75 52 33 50 71
                 \  \   /  /
                  \  \ /  /
                  74 51 72
                    \   /
                     \ /
                     73
.
The 8 nearest neighbors of 4 are 1,3,5,10,11,12,21,23, their sum is 86, so a(4)=86.
		

Crossrefs

Coordinates (but 0-based): A010751, A305258.

Programs

  • Python
    SIZE=17  # must be odd
    grid = [0] * (SIZE*SIZE)
    posX = posY = SIZE//2
    saveX = [0]* (SIZE*SIZE+1)
    saveY = [0]* (SIZE*SIZE+1)
    grid[posY*SIZE+posX]=1
    saveX[1]=posX
    saveY[1]=posY
    posX += 1
    grid[posY*SIZE+posX]=2
    saveX[2]=posX
    saveY[2]=posY
    n = 3
    def walk(stepX, stepY, chkX, chkY):
      global posX, posY, n
      while 1:
        posX+=stepX
        posY+=stepY
        grid[posY*SIZE+posX]=n
        saveX[n]=posX
        saveY[n]=posY
        n+=1
        if grid[(posY+chkY)*SIZE+posX+chkX]==0:
            return
    while posX!=SIZE-1:
        walk(-1,  1, -1, -1)    # down-left
        walk(-1, -1,  1, -1)    # up-left
        walk( 1, -1,  1,  0)    # up-right
        walk( 1,  0,  1,  1)    # right
        walk( 1,  1, -1,  1)    # down-right
    for s in range(1, n):
        posX = saveX[s]
        posY = saveY[s]
        i,j = grid[(posY-1)*SIZE+posX-1], grid[(posY-1)*SIZE+posX+1]
        u,v = grid[(posY+1)*SIZE+posX-1], grid[(posY+1)*SIZE+posX+1]
        if i==0 or j==0 or u==0 or v==0:
            break
        k = grid[(posY-1)*SIZE+posX  ] + grid[(posY+1)*SIZE+posX  ]
        k+= grid[ posY   *SIZE+posX-1] + grid[ posY   *SIZE+posX+1]
        print(i+j+u+v+k, end=' ')
    print()
    for y in range(SIZE):
        for x in range(SIZE):
            print('%3d' % grid[y*SIZE+x], end=' ')
        print()
    
  • Python
    def spiral(x, y):
        r = abs(x) + abs(y)
        return 1 + 2*r*r + (y-r if x > 0 else r-y)
    def A215468(n):
        x = A010751(n-1)
        y = A305258(n-1)
        return sum(spiral(x+i, y+j) for i in (-1, 0, 1) for j in (-1, 0, 1)
                   if (i, j) != (0, 0)) # David Radcliffe, Aug 05 2025