Problem Description
16 |
15 |
11 |
5 |
8 |
17 |
16 |
12 |
6 |
9 |
14 |
13 |
9 |
3 |
6 |
13 |
12 |
8 |
2 |
5 |
28 |
27 |
23 |
17 |
20 |
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
case. )
For example, select (Col 1, Row 2:
17), (Col 2, Row
3:
13), (Col
3, Row 4:
8), (Col 4, Row
1:
5). (Col 5, Row
5:
20).
These numbers
(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
are
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
required.
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