Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
With natural coordinate system solution in Creative category for Square Spiral by Phil15
from math import ceil, sqrt
def coordinates(a):
"""Determine coordinates of 'a' in my coordinate system, where 1 is origin."""
if a==1:
return (0, 0)
#Odd number 'f' is such as 'a' and f**2 are in the same square border.
#In the square where is f**2, we can easily know the coordinates of the 4 vertices.
# 3**2, 5**2, 7**2... have coordinates (-1,1), (-2,2), (-3,3)...
# 3**2-2, 5**2-4, 7**2-6... have coordinates (-1,-1), (-2,-2), (-3,-3)...
# 3**2-2-2, 5**2-4-4, 7**2-6-6... have coordinates (1,-1), (2,-2), (3,-3)...
# 3**2-2-2-2, 5**2-4-4-4, 7**2-6-6-6... have coordinates (1,1), (2,2), (3,3)...
f = ceil(sqrt(a))//2*2+1
x, y = (f**2-a)//(f-1), (f**2-a)%(f-1)
g = (f-1)//2
#We determine the coordinates of 'a' after determine the edge 'x' where is 'a'.
return [(-g, g-y), (-g+y, -g), (g, -g+y), (g-y, g)][x]
def find_distance(first, second):
"""Manhattan distance between first and second, using my coordinate system."""
A0, A1 = coordinates(first)
B0, B1 = coordinates(second)
return abs(B0-A0) + abs(B1-A1)
April 5, 2018
Comments: