Calculates on this device. File exports run online.
Magic Square Generator
Choose a size and press Make the square: every row, column and both diagonals add up to the same magic sum. Make a random variant or a puzzle with empty cells to print, check a square you have, or let the page fill in a puzzle.
Enter your values
Check these values
Your values are worked out in this browser. They are not sent to a server.
Your result will show here
Fill in the values, then choose the button below them.
Your result
How it was worked out
How to make a magic square
- Choose Make a magic square and a size from 3 to 30.
- Under More options you can start from another number, count in steps, or type the magic sum you want.
- Tick Make a random variant for a different square each time, and type a number of Empty cells for a puzzle.
- Press Make the square. In the box below, print the square (and the answer of a puzzle on its own page) at its real size, or download pictures or a document.
The magic sum
With the numbers 1 to n², each row, column and diagonal of an n × n magic square adds up to n(n² + 1) ÷ 2: 15 for 3 × 3, 34 for 4 × 4, 65 for 5 × 5 and 111 for 6 × 6.
With a first number a and a step d, the sum is n × a + d × n(n² − 1) ÷ 2. So a 3 × 3 square of whole numbers in steps of 1 can only have a sum that is a multiple of 3, and a 4 × 4 square a sum that is 2 more than a multiple of 4.
How the squares are made
- Odd sizes (3, 5, 7 …): the Siamese method of de la Loubère. 1 goes in the middle of the top row; each next number goes one up and one to the right, wrapping round the edges; when that cell is taken, it goes one down instead.
- Multiples of 4 (4, 8, 12 …): write 1 to n² row by row, then replace every number on the diagonals of each 4 × 4 block by n² + 1 minus that number.
- Other even sizes (6, 10, 14 …): the LUX method of John Conway. An odd square of half the size is made first; each of its cells becomes a 2 × 2 block filled in an L, U or X pattern.
- Random variants stay magic: mirrored pairs of rows and columns are reordered together, the square may be turned or mirrored, and every number v may be changed to n² + 1 − v.
Puzzles, checks and solutions
- A puzzle leaves some cells empty. For 3 × 3 and 4 × 4 squares the page chooses the empty cells so that only one answer fits; for larger squares it says that this was not checked.
- Check a square adds up every row, column and diagonal of a square you type, and says whether it is magic, semi-magic, normal (1 to n² once each), pandiagonal or associative.
- Fill in a puzzle finds every way to complete a square of 3 × 3 to 6 × 6 with the numbers 1 to n² (or your own list), and shows the first one.
Limits you should know
- The solver tries up to 3,000,000 placements. A puzzle with very many empty cells may need more; the page then says that the count is not complete.
- Squares larger than about 12 × 12 print with small numbers; make the cells larger under More options and print on bigger paper.