Skip to content

Instantly share code, notes, and snippets.

@trueskawka
Created October 15, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueskawka/cc22b1fa5311ff56da8c7bba41888843 to your computer and use it in GitHub Desktop.
Save trueskawka/cc22b1fa5311ff56da8c7bba41888843 to your computer and use it in GitHub Desktop.
infinite matrix brain teaser
def summation(n):
return ((n**2 + n)/2) + 1
def imvalue(i, j):
if ((j + i - 1) % 2 == 0):
return summation(j + i - 1) - i
else:
return summation(j + i - 1) - j
#edges that are starts of streams
print(imvalue(1,3)) #4
print(imvalue(1,5)) #11
print(imvalue(4,1)) #7
print(imvalue(2,1)) #2
#edges that are not starts
print(imvalue(1,4)) #10
print(imvalue(1,6)) #21
print(imvalue(3,1)) #6
print(imvalue(6,1)) #16
#values in the middle of upstream
print(imvalue(2,3)) #9
print(imvalue(2,5)) #20
print(imvalue(4,3)) #18
print(imvalue(3,2)) #8
#values in the middle of downstream
print(imvalue(2,4)) #12
print(imvalue(2,6)) #23
print(imvalue(2,2)) #5
print(imvalue(6,2)) #27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment