Magic Squares through Permutation ( Due 13 Oct 2023 )

A magic square is a square grid of numbers where the sum of each row, the sum of each column, and the sum of each major diagonal are the same. Usually the numbers in a magic square start at 1 and end at n2, where n is the side of the square.

There is a nifty algorithm to generate magic squares of odd order. But the algorithm for generating magic squares of even order is more complicated.

Here is a very famous magic square that is featured in the painting Melancholia by Albrecht Durer (1514).

16   3   2  13
 5  10  11   8
 9   6   7  12
 4  15  14   1

In this assignment, there is no input. You will generate all 3x3 magic squares through permuation. Here are the steps:

  1. Create a 1-D list of integers 1 through 9.
  2. Permute this list of integers.
  3. For each permutation convert the 1-D list into a 2-D list that is 3 x 3.
  4. Check if that 2-D list is a magic square. If it is, then print it out.
  5. Stop when you have all the magic squares.

The magic constant is given by n * (n2 + 1) / 2, where n is the dimension of the magic square. For a 3 x 3 magic square the constant is 15.

Your magic squares must be neatly printed out. Make sure that there is an empty line between each magic square that you print out.

2 7 6
9 5 1
4 3 8

2 9 4
7 5 3
6 1 8

...

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 15 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 as you build the third row, if the first column does not add to 15 then stop the permutation and go to the next one. Similarly, if the second column does not add to the magic constant, stop the permutation and go to the next one.

You should get 8 magic squares. If you hard code any of the answers that will be a zero for this assignment. We are not interested in the answer but how efficiently you obtained the answer through permutation.

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming. .

Use the template MagicSquare.py for your code. We are looking for clean and structured design. The file will have a header of the following form:

#  File: MagicSquare.py

#  Description:

#  Student's Name:

#  Student's UT EID:
 
#  Partner's Name:

#  Partner's UT EID:

#  Course Name: CS 313E 

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

If you are working with a partner then only one of you will submit the program but make sure that you have your partner's name and eid in your program. If you are working alone, then remove the two lines that has the partner's name and eid in the header.

Use the Canvas system to submit your MagicSquare.py file. We should receive your work by 11 PM on Friday, 13 Oct 2023. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.

References