Skip to content

Instantly share code, notes, and snippets.

@aomoriringo
Last active August 29, 2015 14:13
Show Gist options
  • Save aomoriringo/db5cf054330ea432fccf to your computer and use it in GitHub Desktop.
Save aomoriringo/db5cf054330ea432fccf to your computer and use it in GitHub Desktop.
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