Skip to content

Instantly share code, notes, and snippets.

@pavelpatrin
Created October 26, 2017 18:40
Show Gist options
  • Save pavelpatrin/b6363e5aa9c69965f5f3c14144c4d221 to your computer and use it in GitHub Desktop.
Save pavelpatrin/b6363e5aa9c69965f5f3c14144c4d221 to your computer and use it in GitHub Desktop.
def matrix(side_width):
for y in xrange(side_width):
for x in xrange(side_width):
ring_number = min(x, y, side_width - 1 - x, side_width - 1 - y)
ring_serial = x + y - 2 * ring_number
if x >= y:
ring_base = 4 * ring_number * (side_width - ring_number)
result = ring_base + ring_serial
else:
ring_ceil = 4 * (ring_number + 1) * (side_width - ring_number - 1)
result = ring_ceil - ring_serial
sys.stdout.write('%6s' % (result + 1))
sys.stdout.write('\n')
@pavelpatrin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment