IBM ILOG Solver User's Manual > The Basics > Searching with Predefined Goals: Magic Square > Describe > Discussion

As in Chapter 3, Using Arrays and Basic Search: Changing Money, try modeling the problem using equations. Consider a magic square of size n. It contains n2 elements, so that the sum of all its elements is the sum of the first n2 positive integers. The sum of the first n2 natural numbers can be calculated like this: n2(n2 + 1)/2.

Because this is a magic square, each row has the same sum, r. The number of rows equals n. The sum of all the rows, nr, also equals the sum of all the elements. Therefore, nr = n2(n2 + 1)/2 or r = n(n2 + 1)/2. The sum of each column or main diagonal must equal the sum of each row. Therefore, all rows, columns, and main diagonals have a sum of n(n2 + 1)/2.

For example, a magic square of size n = 3 contains n2 or 9 elements. These elements are the first nine natural numbers: 1, 2, 3, 4, 5, 6, 7, 8, and 9. The sum of these elements is n2(n2 + 1)/2 or 45. There are 3 rows. Each row has the same sum, r. The sum of all the rows, 3*r, equals n22(n2 + 1)/2 or 45. Since each row has the same sum, the sum of each row is 45/3 or 15. The sum of each column or main diagonal must equal the sum of each row. Therefore, all rows, columns, and main diagonals have a sum of 15.

You can check this for yourself, using the n = 3 magic square:


 
8  
3  
4  
=15 

 
1  
5  
9  
=15 

 
6  
7  
2  
=15 
=15 
=15 
=15 
=15 
=15 

What is known?

What is unknown?

What are the constraints?