Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was looking at the wikipedia page for the "Magic Square":

and it gave this equation to generate the numbers for a given square:

n((i+j-1+(n/2)) mod n) + ((i+2j-2) mod n)+1 , where i and j are rows and columns

But it doesn't seem to generate the correct numbers for me. For example for a n=3 (3x3) magic square the first element should be 8, but if I put i=1, j=1, n=3 into equation I get 9.5.

What's the deal?

share|cite|improve this question
up vote 1 down vote accepted

There should be $\lfloor\frac{n}{2}\rfloor=\lfloor\frac{3}{2}\rfloor=1$, not $\frac{n}{2}$

share|cite|improve this answer
ahh I thought that was just angle brackets and didn't realize it was the floor function! – eggie5 Feb 10 '12 at 23:58

Also, Matlab has the function magic(N) which will generate a magic square of any size N greater than 0 but not equal to 2.

From Matlab help:

MAGIC Magic square. MAGIC(N) is an N-by-N matrix constructed from the integers 1 through N^2 with equal row, column, and diagonal sums. Produces valid magic squares for all N > 0 except N = 2.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.