Henry Higgins: By George, she's got it, By George, she's got it. (or so I hope). I finally managed to debug the script and changed the solution strategy... and who do you think doesn't have a magic square to show the teacher?
Paul, that laggard!
The magic squares for the other students are:
John = 10 15 8 14
3 16 8
14 9 4
10 2 15
C P H
N I D
J B O
Marty = 13 1 18 20 25
6 25 8
15 13 11
18 1 20
F Y H
O M K
R A T
Sheila = 19 8 5 9 12 1
5 23 8
15 12 9
16 1 19
E W H
O L I
P A S
Smack = 19 13 1 3 11
3 21 9
17 11 5
13 1 19
C U I
Q K E
M A S
Suzy = 19 21 26 25
15 26 19
24 20 16
21 14 25
O Z S
X T P
U N Y
Elsa = 5 12 19 1
3 19 8
15 10 5
12 1 17
C S H
O J E
L A Q
no solution:
Paul (16 1 21 12)
The solution was found by using a little algebra to calculate some contraints on the diagonal of the magic square. This included a maximum possible sum and some contraints on the relationship between the diagonal cell values.
With the diagonal and one other corner, it is
possible to calculate the values in the remaining squares.
So to find the solution, I generated all possible combinations of diagonals. And for every diagonal that fit the constraints, I searched for all possible values of a second corner. Clarification re: tilly below - "all possible combinations" until a diagonal for a valid magic square was found.
On my system the script took 0.69s between 0.40 and 0.48s to print the above output.
The code follows:
Update: moved end of spoiler section.
Update: made two small changes to increase speed by 30-40%
| [reply] [d/l] [select] |
| [reply] |
| [reply] |