### Search

 Search WWW Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway,  consider using this link.

We receive a few cents from each purchase.  Thanks

### Support DFF - Donate

If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

### 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.

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.

### 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 numbers.) 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 age.

 Original Date: December 02, 2002 Modified: May 15, 2018