Monday, February 9, 2009

Magic Squares

A magic square is a square whose cells are filled with numbers such that the sum of each row, column and diagonal is the same. Here's how to create a magic square having three rows. Fill the 9 cells with the numbers 1-9 sequentially. Start with a 1 in the center of the first row (x denotes an as yet unfilled cell):
x1x
xxx
xxx
Now there are two simple rules to follow.
1) Try to move North-East. If there is no available cell there, then wrap around. If we go out of the top of the square, then we wrap downwards; if we go out of the right of the square, then we wrap leftwards. To understand "wrapping" better, imagine the square rolled into a cylinder behind the screen from top down or right to left.
2) If a cell is present to the north-east and if it is already occupied, then enter the next number in the cell directly below the current one. Similar action is taken after filling the cell at the top-right corner of the square.
Using these two simple rules, the cells get filled up like this:
(x1x)---(x1x)---(x1x)---(x1x)
(xxx)---(3xx)---(3xx)---(35x)
(xx2)---(xx2)---(4x2)---(4x2)

(x16)---(x16)---(816)---(816)
(35x)---(357)---(357)---(357)
(4x2)---(4x2)---(4x2)---(492)
The same procedure applies to any magic square having an odd number of rows.
Happy squaring!

1 comment:

  1. I remember doing something like this looong back, looking at a book..
    later lost the book.. and I forgot it also
    thanks for reminding it.. Will try out!

    ReplyDelete