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

  • Context: Undergrad 
  • Thread starter Thread starter NINHARDCOREFAN
  • Start date Start date
  • Tags Tags
    Magic Square
Click For Summary

Discussion Overview

The discussion revolves around the concept of magic squares, specifically how to determine if an n x n grid is a true magic square. Participants explore the definitions, conditions, and methods for checking magic squares, including programming approaches and mathematical properties.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants clarify that a magic square must contain all integers from 1 to n², and each number must appear exactly once.
  • Others suggest creating a 2-D array to check for the presence of each number from 1 to n², using loops in programming.
  • One participant emphasizes that all rows, columns, and diagonals must sum to the same number as a critical condition for a magic square.
  • There is a discussion about the definition of magic squares, with some arguing that the numbers do not necessarily have to be 1 to n², while others assert that this is a common requirement.
  • A method for constructing magic squares for odd n is described, detailing a step-by-step placement of numbers to achieve the magic square properties.
  • Another participant presents a mathematical formulation for generating magic squares, including conditions for the sums involved.
  • A participant introduces a new concept for creating larger magic squares and claims to have developed a three-dimensional version, inviting others to contact them for more information.

Areas of Agreement / Disagreement

Participants express differing views on the requirements for numbers in a magic square, with some insisting on the necessity of using integers from 1 to n², while others argue that this is not a strict requirement. The discussion remains unresolved regarding the broader definition and properties of magic squares.

Contextual Notes

Some participants mention programming languages and methods specific to their knowledge, which may limit the applicability of their suggestions to other programming environments. Additionally, there are unresolved mathematical steps and assumptions regarding the construction and properties of magic squares.

Who May Find This Useful

This discussion may be useful for programmers interested in algorithms for checking magic squares, mathematicians exploring the properties of magic squares, and enthusiasts looking to understand the construction of magic squares in various dimensions.

NINHARDCOREFAN
Messages
118
Reaction score
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
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.
 
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.
 
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.
 
why is that?
 
Parth Dave said:
why is that?

That's just how "Magic Squares" are defined.
 
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.
 
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.
 
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
 

Similar threads

  • · Replies 68 ·
3
Replies
68
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
926
  • · Replies 29 ·
Replies
29
Views
6K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K