1
$\begingroup$

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

http://en.wikipedia.org/wiki/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?

2 Answers 2

1

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

http://en.wikipedia.org/wiki/Floor_and_ceiling_functions

  • 0
    ahh I thought that was just angle brackets and didn't realize it was the floor function!2012-02-10
1

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.