Skip to content

Instantly share code, notes, and snippets.

@GregDMeyer
Last active January 7, 2019 14:21
Show Gist options
  • Save GregDMeyer/e90504e6c735daf18c9e7d180cd562eb to your computer and use it in GitHub Desktop.
Save GregDMeyer/e90504e6c735daf18c9e7d180cd562eb to your computer and use it in GitHub Desktop.
Solution to 538 Riddler "What are these dang bits!"
from matplotlib import pyplot as plt
import numpy as np
from sys import argv
def f(x,y):
'''
Count the number of iterations for Euclid's algorithm computing gcd(x,y),
and return whether it is odd or even.
'''
iters = 0
while 0 not in (x,y):
iters += 1
if x < y:
y = y-x
else:
x = x-y
return iters % 2
def build_array(dim):
'''
Build a dim x dim array of the values of f(i,j)
'''
rtn = np.ndarray((dim, dim), dtype=int)
for i in range(dim):
for j in range(dim):
rtn[i,j] = f(i,j)
return rtn
def main():
if len(argv) < 2:
raise ValueError("Pass desired dimension as first command line arg")
dim = int(argv[1])
ary = build_array(dim)
plt.imshow(ary)
plt.gca().invert_yaxis()
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment