OFFSET
1,3
LINKS
Antti Karttunen, Table of n, a(n) for n = 1..1001
FORMULA
a(n) = (A002024(n^2)-A002024(n)) + |A002260(n^2)-A002260(n)|. [Where |x| stands for the absolute value. This formula can be probably reduced further.] - Antti Karttunen, Jan 25 2014
EXAMPLE
The triangle where we measure distances begins as (cf. A000027 drawn as a lower or upper right triangle):
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
Manhattan distance between 5 and 25 in this triangle is 4+2=6, thus a(5)=6.
PROG
(Python)
import math
def getXY(n):
y = math.isqrt(n*2)
if n<=y*(y+1)//2: y-=1
x = n - y*(y+1)//2
return x, y
def a(n):
ox, oy = getXY(n)
nx, ny = getXY(n*n)
return abs(nx-ox)+abs(ny-oy)
print([a(n) for n in range(1, 66)])
(Python)
from math import isqrt, comb
def A236345(n): return (isqrt(n**2<<3)+1>>1)-(isqrt(n<<3)+1>>1)+abs(n*(n-1)-comb((m2:=isqrt(k2:=n**2<<1))+(k2>m2*(m2+1)), 2)+comb((m:=isqrt(k:=n<<1))+(k>m*(m+1)), 2)) # Chai Wah Wu, Jun 07 2025
CROSSREFS
KEYWORD
nonn,easy
AUTHOR
Alex Ratushnyak, Jan 23 2014
STATUS
approved
