Toolvito

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

Any size from 3 to 30, other numbers or a magic sum you choose, random variants and puzzles to print.

Your values

Reorders, turns or mirrors the square so that it is still magic.

For a puzzle to print: these cells are left empty and the answer comes on its own page.

More options

Optional: the first number is then worked out so that every line adds up to this sum.

From 5 to 50 mm. Empty: up to 20 mm, so that the square fits on a page.

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.

Print or download

Task
Print the square (or the puzzle and its answer)

You get a PDF with the square at its exact size; a puzzle comes on one page and its answer on the next.

More options

Leave it empty to use "magic-square".

The squares are made, checked and solved in your browser. When you press a button in the box below, the drawing of the square (and, for a document, the result lines and the tables) is sent to the Toolvito server to make the file. Download the result within one hour after the job finishes. Choose Delete files now to request earlier removal. The hourly cleanup normally removes files within one to two hours after completion.

How to make a magic square

  1. Choose Make a magic square and a size from 3 to 30.
  2. Under More options you can start from another number, count in steps, or type the magic sum you want.
  3. Tick Make a random variant for a different square each time, and type a number of Empty cells for a puzzle.
  4. 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.

Related tools