﻿ Magic Squares Generator - MadeBits

# Magic Squares Generator

There are a lot of magic squares types, but here, a magic square is a square matrix made of subsequent natural numbers, whose sums for each row, column, and the two main diagonals are equal. Magic squares such as those defined above are called 'perfect squares'.

 1

n=1
 8 1 6 3 5 7 4 9 2

n=3
 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

n=5
 30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20

n=7

The squares shown above are the base perfect squares, where the numbers from 1 to 'N', where 'N' is the number of elements in the matrix (N = n2 e.g., 3x3 = 9), are used only once. Adding the same number to every square element, or multiplying it by the same number, produces other magic squares of the same family.

## Generate your own Magic Square

This online demo generates magic suares of odd sizes (1,3,5,7, ...) , in a new window:

 Matrix size N (NxN, N must be odd): (requires Javascript)

## MagicSQ

MagicSQ implements a simple algorithm (Simon de la Loubère's) for creating squares of the same type as shown above. It can create base perfect squares of any size and is limited only by the memory in your system and by your time. It is a polynomial time algorithm of O(n2) and has a n2 space complexity. The above tables were generated by this program.

The n-odd magic square algorithm is easy to learn by heart and could be great for impressing kids. For an explanation of the algorithm used in the MagicSQ generator see: