OFFSET
1,2
COMMENTS
Spiral begins:
.
49 26--27--28--29--30--31
| | |
48 25 10--11--12--13 32
| | | | |
47 24 9 2---3 14 33
| | | | | | |
46 23 8 1 4 15 34
| | | | | |
45 22 7---6---5 16 35
| | | |
44 21--20--19--18--17 36
| |
43--42--41--40--39--38--37
EXAMPLE
Manhattan distance between 2^3=8 and 3^3=27 is 3 in a square spiral, so a(2)=3.
PROG
(Python)
import math
def get_x_y(n):
sr = int(math.sqrt(n-1)) # Ok for small n's
sr = sr-1+(sr&1)
rm = n-sr*sr
d = (sr+1)/2
if rm<=sr+1:
return -d+rm, d
if rm<=sr*2+2:
return d, d-(rm-(sr+1))
if rm<=sr*3+3:
return d-(rm-(sr*2+2)), -d
return -d, -d+rm-(sr*3+3)
for n in range(1, 77):
x0, y0 = get_x_y(n**3)
x1, y1 = get_x_y((n+1)**3)
print str(abs(x1-x0)+abs(y1-y0))+', ',
CROSSREFS
KEYWORD
nonn
AUTHOR
Alex Ratushnyak, Jan 16 2014
STATUS
approved