Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Last active August 29, 2015 14:13
Show Gist options
  • Save cesarkawakami/064d59c24446653ae1d4 to your computer and use it in GitHub Desktop.
Save cesarkawakami/064d59c24446653ae1d4 to your computer and use it in GitHub Desktop.
import math
m = [
[1, 2, 4, 7, 11, 16],
[3, 5, 8, 12, 17],
[6, 9, 13, 18],
[10, 14, 19],
[15, 20],
[21],
]
def inv(k):
x = int((math.sqrt(8 * k - 7) + 1) / 2)
y = k - x * (x - 1) / 2
x -= y - 1
return x, y
for row in m:
print "\t".join(str(inv(k)) for k in row)
(1, 1) (2, 1) (3, 1) (4, 1) (5, 1) (6, 1)
(1, 2) (2, 2) (3, 2) (4, 2) (5, 2)
(1, 3) (2, 3) (3, 3) (4, 3)
(1, 4) (2, 4) (3, 4)
(1, 5) (2, 5)
(1, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment