OFFSET
1,1
COMMENTS
EXAMPLE
Right triangular type-2 spiral begins:
67
66 68
65 37 69
64 36 38 70
63 35 16 39 71
62 34 15 17 40 72
61 33 14 4 18 41 73
60 32 13 3 5 19 42 74
59 31 12 2 1 6 20 43 75
58 30 11 10 9 8 7 21 44 76
57 29 28 27 26 25 24 23 22 45 77
56 55 54 53 52 51 50 49 48 47 46 78
The eight nearest neighbors of 5 are 1, 2, 3, 4, 18, 41, 19, 6. Their sum is a(5)=94.
PROG
(Python)
SIZE=29 # must be odd
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 1:
walk(-1, 0, 0, -1) # left
walk(0, -1, 1, 1) # up
if posY==0:
break
walk( 1, 1, -1, 0) # right-down
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