Creating a Magic Square Program: How to Check if an n x n Grid is Truly Magical

In summary: So,for example for A=15: | 1 | 4 | 10 | | 2 | 5 | 8 | | 7 | 6 | 3 |And for A=21: | 2 | 9 | 10 | | 4 | 7 | 10 | | 7 | 5 | 9 |In summary, a magic square is defined as an n by n array of numbers where the sum of the numbers in each row, column, and diagonal is the same
  • #1
NINHARDCOREFAN
118
0
"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?
 
Mathematics news on Phys.org
  • #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 don't 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.
 
  • #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.
 
  • #4
NINHARDCOREFAN said:
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.

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.
 
  • #5
why is that?
 
  • #6
Parth Dave said:
why is that?

That's just how "Magic Squares" are defined.
 
  • #7
Parth Dave said:
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 don't 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.

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.
 
  • #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.
 
  • #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 |


where:

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 --->

|2|7|6|
|9|5|1|
|4|3|8|
 
Last edited:
  • #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
 

1. What is a Magic Square?

A Magic Square is a grid of numbers, usually arranged in a square, where the sum of each row, column, and diagonal is the same. It is often used as a puzzle or mathematical game.

2. How do you solve a Magic Square?

To solve a Magic Square, you must place the numbers 1 to n² (n squared) in the grid such that each row, column, and diagonal adds up to the same number. There are various techniques and algorithms that can be used to solve a Magic Square, such as the Siamese method or the Knight's Tour method.

3. What is the history of Magic Squares?

Magic Squares have been around for thousands of years and have been found in various cultures, including ancient China and India. They were often used for divination and considered to have mystical properties. In modern times, Magic Squares have been studied and enjoyed for their mathematical properties and puzzles.

4. Are all Magic Squares the same?

No, there are different types of Magic Squares, such as odd, even, and pan-diagonal Magic Squares. Each type has its own rules and characteristics. Additionally, there are many different variations and sizes of Magic Squares, making each one unique.

5. What is the significance of Magic Squares in science?

Magic Squares have been studied by mathematicians and scientists for their patterns and properties. They have been used to study number theory, geometry, and even quantum mechanics. Magic Squares also have practical applications, such as in coding and encryption methods.

Similar threads

  • General Math
Replies
2
Views
1K
Replies
68
Views
9K
  • General Math
Replies
1
Views
1K
Replies
8
Views
2K
Replies
6
Views
2K
Replies
1
Views
751
Replies
1
Views
634
  • Math Proof Training and Practice
Replies
23
Views
443
Replies
5
Views
2K
  • General Math
Replies
28
Views
3K
Back
Top