Magic Integer Generator

Generates an odd-order magic square using the classic Siamese (De la Loubere) method, then applies a random rotation or reflection for variety, printing the grid alongside the magic constant every row, column, and diagonal sums to. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-27

Overview

Introduction

This tool builds a magic square, a grid of consecutive integers where every row, column, and main diagonal adds up to the same total, using the well-known Siamese method for odd orders.

Choose an odd order from 3 to 9, and it lays out the numbers 1 through order-squared so every line sums to the same magic constant, which is printed alongside the grid.

What Is Magic Integer Generator?

A magic square is an N-by-N grid containing each integer from 1 to N-squared exactly once, arranged so every row, column, and both main diagonals share the same sum, the 'magic constant'.

This generator produces odd-order magic squares (3, 5, 7, or 9) using the Siamese method, one of the oldest and simplest known constructions for odd-order squares.

How Magic Integer Generator Works

Starting from the middle of the top row, each successive number is placed one row up and one column to the right (wrapping around the grid edges); if that cell is already filled, the number instead goes directly below the previous one.

This single deterministic walk always produces a valid magic square for any odd order.

The tool then applies a random rotation (0-270 degrees) and/or horizontal reflection, transformations that preserve every row/column/diagonal sum, so repeated generation looks different while staying valid.

When To Use Magic Integer Generator

Use it for a quick, verifiably correct magic square for a math demo, puzzle handout, or recreational-math writeup.

It's also a clear way to see the Siamese method's construction in action for different odd orders.

Features

Advantages

  • Deterministically correct: every output is a genuine magic square with a verifiable, documented magic constant.
  • Covers all supported odd orders (3 through 9) with the same simple algorithm.
  • Regenerate gives visual variety (rotations/reflections) without ever breaking the magic-square property.

Limitations

  • Only supports odd orders (3-9); even-order magic squares need a different construction and are not implemented.
  • Regenerate only cycles through one base square's 8 rotation/reflection symmetries, not the full space of magic squares for that order.
  • Purely a numeric puzzle generator; it does not solve or verify arbitrary user-supplied grids.

Examples

A 3x3 magic square

Input

3

Output

2 7 6
9 5 1
4 3 8
Magic constant: 15

Every row, column, and diagonal of this classic order-3 square sums to 15, the appended magic constant (exact orientation varies with the random rotation/reflection).

A 5x5 magic square

Input

5

Output

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

For order 5, every line sums to 65 = 5*(25+1)/2, regardless of which rotation/reflection Regenerate lands on.

Best Practices & Notes

Best Practices

  • Verify the magic constant yourself by adding up any row: it should always equal N*(N^2+1)/2 for your chosen order N.
  • Use a monospace font/context so the space-separated grid columns stay aligned.

Developer Notes

Implements the classic Siamese/De la Loubere method (move up-right each step, wrapping around edges, and drop straight down on a collision) to build the base square, then applies a random combination of 90-degree rotations and a horizontal reflection, both sum-preserving symmetries, before printing the grid with an appended magic-constant line.

Magic Integer Generator Use Cases

  • Generating a verified magic square for a math lesson or puzzle handout
  • Demonstrating the Siamese method's construction step by step
  • Seeding test fixtures for magic-square-checking code

Common Mistakes

  • Requesting an even order (like 4 or 6); only odd orders 3-9 are supported.
  • Assuming Regenerate explores every possible magic square of that order; it only cycles through one square's symmetries.
  • Forgetting that the appended 'Magic constant:' line is part of the output, not a row of the grid.

Tips

  • Try order 9 for a larger grid with more numbers to verify the magic constant across.
  • Click Regenerate repeatedly to see all 8 rotation/reflection variants of the same base square.

References

Frequently Asked Questions