OFFSET
1,1
COMMENTS
EXAMPLE
Right triangular type-3 spiral begins:
78
77 46
76 45 47
75 44 22 48
74 43 21 23 49
73 42 20 7 24 50
72 41 19 6 8 25 51
71 40 18 5 1 9 26 52
70 39 17 4 3 2 10 27 53
69 38 16 15 14 13 12 11 28 54
68 37 36 35 34 33 32 31 30 29 55
67 66 65 64 63 62 61 60 59 58 57 56
The eight nearest neighbors of 5 are 1, 3, 4, 17, 18, 19, 6, 8. Their sum is a(5)=76.
PROG
(Python)
SIZE=28 # must be even
grid = [0] * (SIZE*SIZE)
saveX = [0]* (SIZE*SIZE)
saveY = [0]* (SIZE*SIZE)
saveX[1] = saveY[1] = posX = posY = SIZE//2
grid[posY*SIZE+posX]=1
n = 2
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 posY==0 or grid[(posY+chkY)*SIZE+posX+chkX]==0:
return
while posY!=0:
walk( 1, 1, -1, 0) # right-down
walk(-1, 0, 0, -1) # left
walk(0, -1, 1, 1) # up
for n in range(1, 92):
posX = saveX[n]
posY = saveY[n]
k = grid[(posY-1)*SIZE+posX] + grid[(posY+1)*SIZE+posX]
k+= grid[(posY-1)*SIZE+posX-1] + grid[(posY-1)*SIZE+posX+1]
k+= grid[(posY+1)*SIZE+posX-1] + grid[(posY+1)*SIZE+posX+1]
k+= grid[posY*SIZE+posX-1] + grid[posY*SIZE+posX+1]
print k,
CROSSREFS
KEYWORD
nonn,easy
AUTHOR
Alex Ratushnyak, Jul 08 2012
STATUS
approved