Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Magic Square question

  1. May 4, 2004 #1
    "The arrangement of numbers with n rows and n columns to be a true magic square is that it contains all the integers 1, 2, 3, n(squared)"

    What does this mean? I have to write a program that checks to see a n rows and columns is a magic square, how do I check for this?
  2. jcsd
  3. May 4, 2004 #2
    what they are asking for is that the set of numbers must have each number from 1, 2, 3.. n^2. u can also notice that the set of data has room for n^2 values (n rows, n columns.. n^2 = number of values inputtable [is that a word?]). Which leaves you to assume that each value must be located atleast once in the entire set of values. Once and only once.

    In order to check this you can create a 2-d array (i only know how to program in java, pascal, and vb, so dont ask me if you are using anything else) and create a loop which will repeat itself until checking each value once.

    first create a 2-d array with n rows and n columns and fill it with whatever data is necessary.
    in order to check if it is a magic square create a for loop that goes from 1 to n^2 and checks to see if each value is located somewhere within the array. Than u can have a boolean check, set it to true and if a single value is not found that change it to false.
  4. May 4, 2004 #3
    Too much info.

    I just wanted to know what 1, 2, 3... n^2 meant and if it was a condition, how do you do that, that's it. Anyway thanks.
  5. May 4, 2004 #4


    User Avatar
    Science Advisor

    Well for example if n=3 then the magic square is 3x3 and must contain the the numbers 1,2,3,4..9.

    What hasn't been mentioned but is a critical part of the definition is that, all rows, all colums and both major diagonals MUST ALL ADD TO THE SAME NUMBER.
  6. May 4, 2004 #5
    why is that?
  7. May 5, 2004 #6


    User Avatar
    Science Advisor

    That's just how "Magic Squares" are defined.
  8. May 5, 2004 #7


    User Avatar
    Staff Emeritus
    Science Advisor

    You seem to have a completely wrong idea of what a "magic square" is. A magic square is DEFINED as an n by n array of numbers such that the sum of the numbers on any row, column, or diagonal is the same. It is not, in general, necessary that the number be 1, 2, ..., n2 although those are the most common.
  9. May 5, 2004 #8
    There is a method to creating magic squares which could allow you to check them faster. This only works if n is odd in an n X n square.

    This should also give you a better idea of what a magic square is, too.

    First, start at one and place the number in the middle column (for example, if you had a 5 row x 5 column square, it would go in the 3rd column). Then, move one box to up, and one box to the left. If you're already in the top row, go to the bottom row. If you're already on the left-most row, go to the right-most row. If the box you are at is already filled with a number, drop one row and put the number there. Then, go up 1, left 1 until you fill use up all the numbers.

    For example, with a 3x3...
    _ _ _
    _ _ _
    _ _ _

    1. Put in the 1 in the top middle column:

    _ 1 _
    _ _ _
    _ _ _

    2. Up 1, left 1.
    _ 1 _
    _ _ _
    2 _ _

    3. Repeat
    _ 1 _
    _ _ 3
    2 _ _

    4. Now, up one and left one takes you to where the "1" already is. So, drop down one row.
    _ 1 _
    _ 4 3
    2 _ _

    5. Keep repeating...
    5 1 _
    _ 4 3
    2 _ _

    5 1 _
    _ 4 3
    2 _ 6

    5 1 _
    _ 4 3
    2 7 6

    Lastly, the final 3x3 "magic square..."
    5 1 9
    8 4 3
    2 7 6

    All rows, columns, and diagonals add up to 15.
  10. May 6, 2004 #9
    For the 3x3 square (the number used being integers or not,also not necessarily different) I worked out once (solving a system of equations) the following general solution:

    | (a+b)/2 | (2/3)*A-b | [2*A+3*(b-a)]/6 |

    | (2/3)*A-a | A/3 | a |

    |(A/3)+(a-b)/2 | b | [4*A-3*(a+b)]/6 |


    A=the sum;must be divisible by 3 for a magic square,we can have A=3,6,9,12,15,18,21,24,27.
    a,b=positive integers or nonintegers,for a magic square a,b=integers,a must be different from b and 1<=a<=9;1<=b<=9;(a+b) and (a-b) must be divided by 2.

    From here,after setting A appropriately,we can find different variants of magic squares;for example if we set A=15 and a=1 and b=3 --->

    Last edited: May 6, 2004
  11. Jul 9, 2007 #10
    Hello there!
    I'm form Lahore - Pakistan.
    Magic Squares are basically a simple Arithmetic Sequence in 2 Dimensional Form. I've recently devised a new concept into this with the help of which I can create even larger patterns of them, like 100x100, 165x165 or 256x256.
    Now look at this smallest possible 3x3 pattern.

    6 7 2
    1 5 9
    8 3 4
    The summation of numbers in any horizontal, vertical line or either of two main diagonals is 15.
    Mathematically, it will look like this.

    n/2[2a+(n-1)xd] diveded by total no of squares
    where a=1 , n=9 & d=1
    Add all numbers from 1 to 9 with the help of above formula & divide it by total no of suqares and there's your answer. I've also succeeded in doing it Three Dimensionally. You may contact me via email & I'll send you some photographic prrof of my 3D model.
    Which means that I have developed Three Dimensional Arithmetic Sequence with the help of Magic Squares & Cubes.
    Bye for now!
    Qaiser Raza
    Lahore - Pakistan
    email : htc_leo_786@yahoo.com
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Magic Square question
  1. Magic Squares (Replies: 8)