Can you place every number from 1 to 25 in a 5x5 grid such that it contains four 3x3 semimagic squares? A semimagic square is a square whose rows and columns all sum to the same number. This is intended to be solved with a computer.

  • $\begingroup$ Can a semimagic square be composed of any 3-subset of rows and any 3-subset of columns? $\endgroup$
    – RobPratt
    Mar 11 at 0:01
  • $\begingroup$ @RobPratt no they need to be contiguous subsets, ie. next to each other. $\endgroup$ Mar 11 at 0:29
  • $\begingroup$ So there are nine 3x3 squares and you want four of them to be semimagic? $\endgroup$
    – RobPratt
    Mar 11 at 0:30
  • $\begingroup$ yes that's correct $\endgroup$ Mar 11 at 0:31

3 Answers 3


Yes, $4$ is the most possible (take the subsquares with $12$, $3$, $18$, and $13$ in their top-left corners as the semi-magic squares). \begin{matrix} 12& 24& 3& 25& 11 \\ 9& 7& 23& 10& 6 \\18& 8& 13& 4& 22 \\20& 14& 5& 19& 15 \\1& 17& 21& 16& 2 \\\end{matrix}

  • $\begingroup$ Yes you got it! I am very impressed at how fast you got that. Interestingly it is a variant of the same square that I found. I wonder if there are other examples? Also I would be keen to see your approach. $\endgroup$ Mar 11 at 0:32
  • 1
    $\begingroup$ I just finished some Python code to enumerate the examples. Up to the various symmetries (rotation, reflection, inversion through 13, and permuting the first and second or fourth and fifth rows and/or columns), this square and RobPratt's square are the only solutions. $\endgroup$ Mar 11 at 18:08

Here's a different one, obtained via integer linear programming:

 18 19  1 12 25
 17  5 16 20  2
  3 14 21  6 11
 13 15 10 24  4
 22  9  7  8 23

  • 1
    $\begingroup$ Fun fact: exchanging the first two rows adds a magic antidiagonal in the top left, and it's the only way to make a magic diagonal. $\endgroup$ Mar 11 at 19:23

Logic underpinning the other answers:

If two subsquares share a full row and/or column, their semimagic sums are equal. Consequently, no two subsquares share two full rows and/or columns, because their third rows/columns would be forced to be equal. We are left with three possible configurations, up to rotation: Zero Corners (top), Three Corners (left), and Four Corners (right), where subsquares of the same color have the same sum.
enter image description here

Four Corners is the only valid configuration.
In the Zero Corners configuration, R1C2-4 must have the same sum as R4C2-4, but R1Cn differs from R4Cn by the difference in sum between the green and red squares. Consequently, the green and red sums are equal, and identical numbers abound.
In the Three Corners configuration, R2C5 and R5C2 are equal*, being both different from R2C2 by the difference between the red and green sums. Likewise for R1C4 and R4C1 (via R4C4).
*(R1C1 is the top left.)

The central cell is congruent to 5 modulo 8 (i.e. 5, 13, or 21).
In the Four Corners configuration, the values of the entire 5×5 square are determined by the central 3×3 as follows, where $S$ is the magic sum:
\begin{array}{ccccc} a+b+d+e-S & S-a-d & S-b-e & S-c-f & b+c+e+f-S\\ S-a-b & a & b & c & S-b-c\\ S-d-e & d & e & f & S-e-f\\ S-g-h & g & h & i & S-h-i\\ d+e+g+h-S & S-d-g & S-e-h & S-f-i & e+f+h+i-S \end{array}
Summing the entries yields $325 = 8S+e$ and reducing modulo 8 yields the claim.

The number of solutions is divisible by 128.
The Four Corners configuration has five validity-preserving symmetries due to its structure:
- Rotation and reflection (order 8)
- Interchanging rows 1 and 2 (order 2)
- Interchanging rows 4 and 5 (order 2)
- Interchanging columns 1 and 2 (order 2)
- Interchanging columns 4 and 5 (order 2)
In fact, there are exactly four solutions up to these symmetries (i.e. 512 in total) - user1502040's, RobPratt's, and the squares formed by replacing each x with 26-x in either of those squares.

  • $\begingroup$ Thanks for that interesting insight! $\endgroup$ Mar 12 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.