Question: MUST BE IN PYTHON: Assignment: Create even magic squares of size 4x4 Here is a very famous magic square that is featured in the painting
MUST BE IN PYTHON:
Assignment: Create even magic squares of size 4x4
Here is a very famous magic square that is featured in the painting Melancholia by Albrecht Durer.
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
In this assignment we will generate all magic squares of order 4 through permutation. The process is straight forward but time consuming. Here are the steps:
Create a 1-D list of integers 1 through 16.
Permute this list of integers.
For each permutation convert the 1-D list into a 2-D list that is 4 x 4.
Check if that 2-D list is a magic square. If it is, then print it out.
The magic constant is given by n * (n2 + 1) / 2, where n is the dimension of the magic square. For a 4 x 4 magic square the constant is 34.
In this program there is no user input. You may reuse any code that you have written for the first assignment. Your magic squares must be neatly printed out. Make sure that there is an empty line between each magic square that you print out.
After everything is working correctly, optimize your code so that it does not go through all the permuations. For example, if the first row does not add to the magic constant 34 stop that permutation and go to the next one. If the second row does not add to the magic constant then stop that permutation and go to the next one. And similarly for the sum of the third row.
Permutation code: def permute (a, lo): hi = len(a) if (lo == hi): print (a) else: for i in range (lo, hi): a[lo], a[i] = a[i], a[lo] permute (a, lo + 1) a[lo], a[i] = a[i], a[lo]
Code from assignment 1:
#this function makes the magic square def make_square (n): #filling in a grid of 0's square = [] for row in range(n): rows = [] for col in range(n): rows.append(0) square.append(rows)
id_row = 0 id_col = 0
# setting up the rules for making a magic square for i in range(1, n**2+1): if i == 1: square[n -1][n // 2] = 1 id_row = n - 1 id_col = n // 2 else: square[id_row][id_col] = i id_row2 = index(id_row, n) id_col2 = index(id_col, n) if square[id_row2][id_col2] > 0: id_row = id_row - 1 id_col = id_col elif (id_row == n - 1) and (id_col == n - 1): id_row = id_row - 1 id_col = id_col - 1 else: id_row = id_row2 id_col = id_col2 return square
#this function changes the index used for the make_square function def index(ind, n): ind += 1 if ind > n - 1: ind = 0 return ind
#this function checks to see if the square created is a magic square def check_square( magic_square ): n = make_square( magic_square) #checks the sum of rows for col in range(len(n)): counter = 0 for i in range(len(n)): counter = counter + n[i][col] if counter != int(len(n)*((len(n)**2)+1) / 2): return False for i in range(len(n)): for j in range(i): if sum(n[i]) != int(len(n)*((len(n)**2)+1) / 2): return False elif sum(n[i][j]) != int(len(n)*((len(n)**2)+1) / 2): return False elif sum(n[j][i]) != int(len(n)*((len(n)**2)+1) / 2): return False return True #this function prints the created square def print_square( magic_square ): for i in magic_square: for j in i: print(format(j, "2d"), end = " ") print()
def main(): n = int(input("Please enter an odd number: ")) while ((n % 2) == 0) or (n < 1): n = int(input("Please enter an odd number: "))
square = make_square(n) checkedsquare = check_square(n) if checkedsquare == True: print() print("Here is a ", n, " x ", n, " magic square: ") print() print_square(square) print() print("This is a magic square and the canonical sum is ", int(n*((n**2)+1)/2)) else: print("This is not a magic square")
if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
