ForHosting KIT · Math

Magic square generator

Build an odd-order magic square in which every row, column and main diagonal has the same sum: the 3×3 square adds up to 15 and the 5×5 to 65. Pick any odd size from 3 to 99 and get the full grid. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Order
3
You get
Magic constant
15
Cells
9

A magic square of odd order n is an n-by-n grid that holds each integer from 1 through n squared exactly once, so that every row, every column, and both main diagonals add to the same magic constant: n times (n squared plus one) divided by two. This generator builds that grid with the Siamese method, also called de la Loubère's method. It places 1 in the middle of the top row and then walks one cell up and one cell right, wrapping at the borders and dropping one cell below when the next cell is already filled. The only required input is the odd order, which must be an integer greater than or equal to 3.

How the Siamese method fills an odd-order square

The Siamese method, written down by Simon de la Loubère after a stay in Siam in the seventeenth century, is the standard constructive proof that a magic square exists for every odd order. Start with an empty n-by-n board and write the integer 1 in the centre cell of the first row. From any cell that already holds k, the candidate home for k plus one is one row above and one column to the right.

If that step leaves the board, wrap around: leaving the top row re-enters at the bottom of the same column walk, and leaving the last column re-enters at the first column of the same row walk. If the wrapped cell is already occupied — which happens after every complete wrap of n steps — do not take the diagonal step at all; instead write the next integer in the cell immediately below the current one and continue from there.

Because n is odd, that collision rule never fights the wrap, and the walk visits every cell exactly once before n squared is placed. The finished grid is magic: each row, each column, and both long diagonals sum to the magic constant n times (n squared plus one) over two. For order 3 that constant is 15 and the unique (up to rotation and reflection) normal square is eight, one, six on the first row, three, five, seven on the second, and four, nine, two on the third.

For order 5 the constant is 65 and the first row is seventeen, twenty-four, one, eight, fifteen.

Where an odd-order magic square generator earns its keep

Classrooms and tutoring platforms use a magic square generator so learners can check a 3-by-3 or 5-by-5 they just built by hand without flipping a wrap-around error in the last few cells. Puzzle sites and worksheet factories need a deterministic oracle that always emits the same Siamese grid for a given n, so answer keys stay stable across reprints and locales. Invalid input does not consume balance.

If you need even-order squares, rotations of the Siamese grid, or a checker that verifies an arbitrary filled board, that is a different search intent and a different capability; this one exists to construct the odd-order Siamese square people look up under magic square generator.

Classroom and worksheet keys

Generate the canonical 3-by-3 or 5-by-5 Siamese square so students can check every row, column, and diagonal against the printed magic constant.

Balanced game boards

Place tokens or scores from a magic square so every row, column, and long diagonal of an odd board carries the same total.

Why must the order be an odd integer greater than or equal to 3?

The Siamese up-right walk is a complete construction only for odd n.

What does the 3×3 Siamese square look like?

The canonical grid is [[8,1,6],[3,5,7],[4,9,2]]. Every row, column, and both main diagonals sum to the magic constant 15.

Does the generator pick a random rotation each time?

No. The walk is fixed: 1 always starts in the middle of the top row and steps up-right. The same order always returns the same two-dimensional array.

How is the magic constant of a square calculated?

Multiply n by n squared plus one, then divide by two. For a 3×3 square that gives 15 and for a 5×5 square it gives 65: every row, every column and both main diagonals add up to that number.

How does the Siamese method place the numbers?

It writes 1 in the middle of the top row, then moves one cell up and one to the right for each next number, wrapping around the edges. When that cell is already taken, it drops to the cell just below instead.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/math/magic-square

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/math/magic-square \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"order":3}'
{
  "order": 3
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "math.magic_square",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_order99
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Even orders, the trivial order 1, fractions, and values above the published maximum are rejected as invalid_input.

How the Siamese method fills an odd-order square

This endpoint always emits that canonical Siamese orientation; it does not pick a random association or a Dürer even-order construction.

Input contract, response shape, and what is rejected

The domain is the odd integers from 3 through 99 inclusive. Even values such as 4 or 8 fail for the same reason — the Siamese up-right walk is not a complete construction for even n, and this capability does not silently switch to a Strachey or LUX algorithm. Values above 99 fail because the declared limit is 99. Optional output keys are omitted rather than sent as null. Failed validation is not billed. POST a JSON body with a single required field, order, the odd integer n of the square. The aliases n and size are accepted so forms and query strings can reuse one contract. The value may arrive as a JSON integer or as a whole decimal string; empty strings, missing fields, fractions, scientific notation, and non-numeric types fail with invalid_input naming order. Order 1 is odd but is not a classroom magic square in the usual 1-through-n-squared sense that people search for, so it is rejected with the same message as an even order: order must be an odd integer greater than or equal to 3. On success the body echoes order, returns square as an array of n rows each of n positive integers, reports magic_constant as n times (n squared plus one) over two, reports cells as n squared, and sets method to the string siamese so callers can record which construction they received. Pair the grid with your own renderer if you need colouring or animation; this endpoint returns numbers, not an image. Keep requests inside the published maximum so the response stays a small JSON document rather than a giant matrix dump.

Where an odd-order magic square generator earns its keep

Game designers sometimes seed a board from a magic square so that every line is balanced; calling a tiny parse API avoids embedding a one-off implementation that later drifts from the textbook walk.

Puzzle and kata fixtures

Seed coding exercises and property tests with a deterministic n-by-n grid whose line sums are n(n²+1)/2, without hard-coding tables.

How do I generate a magic square of odd order?

Send an odd integer order n ≥ 3 (aliases n and size). The API returns an n×n grid filled by the Siamese method, plus the magic constant n(n²+1)/2.

What does the magic square generator cost?

The browser tool is free. API requests use prepaid balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →