Last active
December 25, 2017 20:23
-
-
Save samclearman/64fd5f4d9b1c20e14f64662d88d1c425 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# Infinite queens | |
# Prints the lex-minimal infinite nonattacking queen placement | |
# in the first quadrant - amazingly, they form two lines with slopes | |
# phi and 1/phi | |
import sys | |
N = int(sys.argv[1]) | |
queens = [] | |
def attacking(q,r): | |
if q[0] == r[0]: | |
return True | |
if q[1] == r[1]: | |
return True | |
if (q[0] - r[0]) == (q[1] - r[1]): | |
return True | |
if (q[0] - r[0]) == (r[1] - q[1]): | |
return True | |
return False | |
for i in xrange(N): | |
j = 0 | |
while any(attacking(q, (i,j)) for q in queens): | |
j += 1 | |
queens.append((i,j)) | |
for q in reversed(queens): | |
print ("-" * q[1]) + "Q" + "-" * (int(1.7*N) - q[1]) + "%.2f" % ((1.0 + q[1]) / (1.0 + q[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment