Created
August 13, 2017 19:42
-
-
Save beefy/21772096fad31191ccb9797f93d8b38a to your computer and use it in GitHub Desktop.
Try to find a 3x3 magic square
This file contains hidden or 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
# written by: https://github.com/beefy | |
# for: https://www.reddit.com/r/math/comments/6tcrxs/3x3_magic_square_of_squares_1000_prize/ | |
import numpy as np | |
while(1): | |
# generate a random 3x3 matrix | |
parker = np.random.randint(1000, size=(3, 3)) | |
row_sum = parker.sum(axis=0) | |
col_sum = parker.sum(axis=1) | |
sum_diag = np.trace(parker) | |
# check if the sum of the rows is equal | |
# check if the sum of the columns is equal | |
# check if the sum of each diagonals is equal | |
# check if they are all equal | |
if np.equal.reduce(row_sum) and \ | |
np.equal.reduce(col_sum) and \ | |
sum_diag == np.trace(np.flip(parker,axis=1)) and \ | |
row_sum[0] == col_sum[0] == sum_diag: | |
# we found a solution! | |
# write it down! | |
print "SOLUTION: ", parker | |
file_out = open("solutions.txt","a+") | |
file_out.write(str(parker)) | |
file_out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment