[go: up one dir, main page]

login
A215468
Sum of the 8 nearest neighbors of n in a rotated-square spiral with positive integers.
5
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,1
EXAMPLE
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.
PROG
(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,
print
for y in range(SIZE):
for x in range(SIZE):
print '%3d' % grid[y*SIZE+x],
print
CROSSREFS
Coordinates (but 0-based): A010751, A305258.
Sequence in context: A062118 A167329 A179796 * A109552 A206263 A007692
KEYWORD
nonn,easy
AUTHOR
Alex Ratushnyak, Aug 11 2012
STATUS
approved