Create a gist now

Instantly share code, notes, and snippets.

@aomoriringo /result
Last active Aug 29, 2015

What would you like to do?
uzumaki
>>> from uzumaki import *
>>> [uzumaki(x) for x in range(20)]
[(0, 0), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), (2, -1), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2), (-1, 2), (-2, 2), (-2, 1), (-2, 0), (-2, -1)]
>>> uzumaki(1000000000)
(1683, -15811)
from math import sqrt, floor
def uzumaki(n):
base = int(floor(sqrt(n)))
delta = n-base**2
if base%2 == 0:
basex = -base/2
basey = base/2
xdir = 1
ydir = -1
else:
basex = base/2+1
basey = -base/2+1
xdir = -1
ydir = 1
x = basex + xdir * (delta-base if delta>base else 0)
y = basey + ydir * (delta if delta<base else base)
return x,y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment