Select five numbers from the matrix at right such
that exactly one number is selected from each row and exactly one number
is selected from each column. The sum of the numbers you chose will always equal the "magic number" for the matrix!
(My age in this
For example, select (Col 1, Row 2:
17), (Col 2, Row
3, Row 4:
8), (Col 4, Row
5). (Col 5, Row
(17+13+8+5+20) add up to a value I try not to think about too often.
This program will generate matrices of different sizes and with any
"magic number" you select. (And make sure you follow the
rules cells by erasing the non-selected cells in the row and
column of each selection.)
Background & Techniques
This is another goody from Martin Gardner - from his book "Hexaflexagons
and Other Mathematical Diversions".
Examining the matrix above may provide you with a clue about why this
works. If not, the program contains and explanation.
Gardner points out that any 4X4 grid selected
from a wall calendar will form a magic matrix who's magic number is
twice the sum of the opposite diagonal corners.
Non-programmers are welcome to read on, but may
want to skip to the bottom of this page to
download an executable version of the program.
The program uses a TStringGrid to display square matrices from
3X3 to 6X6 with any user specified "Magic number".
When the user clicks a square, all other numbers in that row and column
erased, and the clicked number is highlighted. A two
dimensional array of integers, GridMask, is used to keep track of
the display status of each cell. Gridmask values are: 0:
display normally (unselected cells), 1, display with green
background (selected cells), -1: display silver background with no number
(cells in same row or column as a selected cell). If the
boolean "Explainmode" flag is true, we are playing a
matrix from the Explain dialog page which uses Column 0 and Row 0 to
display the numbers used to generate the matrix. In this case,
GridMask values of 2 identify these generating numbers so that they may be drawn
with a yellow background. An OnClick exit is used to set
GridMask values and an OnDrawCell exit is used to draw the cells as
This program also includes a version of the "AdjustGridSize"
procedure that sets the height and width of a passed stringgrid to
exactly the dimensions required by the cells.
Running/Exploring the Program
Suggestions for Further Explorations
||One of the tricky tasks in writing the program
was generating the values to fill the grid. The current version
"cheats" by using positive and negative numbers, but it is
certainly possible to generate grids using only positive numbers - even if
small magic numbers result in 0 value cells. How would you
generate random magic matrices using only positive numbers? (Addendum - a
last minute change generates up to 1000 grids looking for one with
all positive numbers before giving up and using one with negative
||Another Gardner suggestion - How about creating a
playable birthday card? Give the recipient a matrix
and instructions. The magic number will of course be his
|Original Date: December 02,
Modified: February 18, 2016