Last active
August 29, 2015 14:13
-
-
Save aomoriringo/db5cf054330ea432fccf to your computer and use it in GitHub Desktop.
uzumaki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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