Cover page images (keyboard)

Project 2 - Magic Squares


design2.txt - due 12/7 before 11:59 PM
project2 - due 12/13, before 11:59 PM

Orginally by Sue Evans

Hit the space bar for next page

The Objectives


The purpose of this assignment is to give you practice with:

The Background

According to The Math Forum @ Drexel University, A Magic Square is an arrangement of the numbers 1 to n2 in an n x n matrix with each number occuring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum is n(n2 + 1)/2.

The simplest magic square is the 1 x 1 magic square whose only entry is the number 1.

1

The next simplest is the 3 x 3 magic square.

(8 + 1 + 6) or 3(32 + 1)/2
This is also just ONE of many examples for a 3 x 3 Magic Square!!

This 3 x 3 square is definitely magic and satisfies the definition given above. There are 8 different arrangements of entries in a 3 x 3 matrix that fit the definition of a magic square. There are also magic squares of size 4 x 4, 5 x 5, etc.

The Background - Continued

For small n, say n <= 3, you could obviously sit down with pencil and paper and discover a magic square in a fairly short amount of time.
Take a few minutes now and do that, remembering that each of the numbers, 1 through n2, appear only once in the square.

Now try it for n = 4 ...

Oh, there has to be a better way. :)

Of course, the computer doesn't have a patience problem. It can solve the problem for us by using the brute force method. This means that the computer will accomplish its task by trying every single possible arrangement of the numbers in a square and check each of the resulting squares to see if it's a magic square.

[6, 2, 7, 5, 4, 8, 3, 9, 1]
[4, 2, 7, 6, 5, 8, 3, 9, 1]
[7, 4, 2, 6, 5, 8, 3, 9, 1]
...

The Task

Your assignment will be to determine all of the magic squares when given an n, display permutations that match the magic squares to the screen AND write it to a file.

Your program must come up with every possible arrangement (permutation) of numbers within the square. These are known as the permutations of that list of numbers. Each permutation needs to be checked to see if it has formed a magic square.

Program Requirements

Sample Run


Here is a sample of output of this program. Please note that the sample output only shows what is expected from your program if the user is actually entering everything as instructed. This is not a test of the program at all, but just a sample for your clarification.
2   7   6
9   5   1
4   3   8

2   9   4
7   5   3
6   1   8

4   3   8
9   5   1
2   7   6

4   9   2
3   5   7
8   1   6

6   1   8
7   5   3
2   9   4

6   7   2
1   5   9
8   3   4

8   1   6
3   5   7
4   9   2

8   3   4
1   5   9
6   7   2

Total running time: 4.76 seconds.

Submitting your work

You must be logged into your account and in the same directory as the file you are trying to submit.

To submit your design, type the following at the linux prompt:

submit cs201 Proj2 design2.txt

To submit your project, type the following at the linux prompt:

submit cs201 Proj2 proj2.py

To verify that your files were submitted, you can execute the following command at the Unix prompt. It will show all files that you submitted in a format similar to the Unix 'ls' command.

submitls cs201 Proj2