Link to home
Start Free TrialLog in
Avatar of Mateen
Mateen

asked on

Formula to fill any magic square.

I don't know how and when I happened to find a technique to fill
any odd square (with odd number of lines 5x5 or 7x7 or 9x9 etc)
with numbers from 1 to number of squares so that
sum of each row
and
sum of each column
and
sum of both diagonal is always the same.

I also don't know whether it is already discussed in  EE Puzzle area.


The method is to identify the next square to write numbers sequentially starting with 1.
Starting point can self be understood.


For example let's consider a 5x5 square

** ** ** ** **
xx xx xx xx **
xx xx xx xx **
xx xx xx xx **
xx xx xx xx **


The squares marked by ** may be called KeySquares
KeySquares at top of the row may be called KeyRowSqr
KeySquares at the last column may be called KeyColSqr

Rules
a) When square is not key square always move diagonally up.
b) When square is not key square and there is no room to move diagonally up shift one square left.
c) When square is KeyColSqr move horizontally left to reach first column and move one square up.
   this will form an L (Reverse)
d) When square is KeyRowSqr move vertically down to reach last row and move one square right.                
   this will form an L (proper)


1 Starting Point
---------------
** ** ** ** **
xx xx xx xx **
xx xx xx xx  1
xx xx xx xx **
xx xx xx xx **

2 RULE C      
--------------
** ** ** ** **
 2 xx xx xx **
xx xx xx xx  1
xx xx xx xx **
xx xx xx xx **

3 RULE A
---------------
**  3 ** ** **
 2 xx xx xx **
xx xx xx xx  1
xx xx xx xx **
xx xx xx xx **

4 RULE D
--------------
**  3 ** ** **
 2 xx xx xx **
xx xx xx xx  1
xx xx xx xx **
xx xx  4 xx **

5 RULE A
--------------
**  3 ** ** **
 2 xx xx xx **
xx xx xx xx  1
xx xx xx  5 **
xx xx  4 xx **


6 RULE B
--------------
**  3 ** ** **
 2 xx xx xx **
xx xx xx xx  1
xx xx  6  5 **
xx xx  4 xx **


7,8 RULE A
--------------
**  3 ** ** **
 2 xx xx xx  8
xx xx xx  7  1
xx xx  6  5 **
xx xx  4 xx **


9 RULE C
--------------
 9  3 ** ** **
 2 xx xx xx  8
xx xx xx  7  1
xx xx  6  5 **
xx xx  4 xx **

10 RULE D
--------------
 9  3 ** ** **
 2 xx xx xx  8
xx xx xx  7  1
xx xx  6  5 **
xx 10  4 xx **

11 RULE B
--------------
 9  3 ** ** **
 2 xx xx xx  8
xx xx xx  7  1
xx xx  6  5 **
11 10  4 xx **


12,13,14,15 RULE A
------------------
 9  3 ** ** 15
 2 xx xx 14  8
xx xx 13  7  1
xx 12  6  5 **
11 10  4 xx **


16 RULE B
---------------
 9  3 ** 16 15
 2 xx xx 14  8
xx xx 13  7  1
xx 12  6  5 **
11 10  4 xx **

17 RULE D
--------------
 9  3 ** 16 15
 2 xx xx 14  8
xx xx 13  7  1
xx 12  6  5 **
11 10  4 xx 17


18 RULE C
--------------
 9  3 ** 16 15
 2 xx xx 14  8
xx xx 13  7  1
18 12  6  5 **
11 10  4 xx 17


19,20 RULE A
--------------
 9  3 ** 16 15
 2 xx 20 14  8
xx 19 13  7  1
18 12  6  5 **
11 10  4 xx 17

21 RULE B
--------------
 9  3 ** 16 15
 2 21 20 14  8
xx 19 13  7  1
18 12  6  5 **
11 10  4 xx 17

22 RULE A
--------------
 9  3 22 16 15
 2 21 20 14  8
xx 19 13  7  1
18 12  6  5 **
11 10  4 xx 17

23 RULE D
--------------
 9  3 22 16 15
 2 21 20 14  8
xx 19 13  7  1
18 12  6  5 **
11 10  4 23 17

24 RULE A
--------------
 9  3 22 16 15
 2 21 20 14  8
xx 19 13  7  1
18 12  6  5 24
11 10  4 23 17

25 RULE C
--------------
 9  3 22 16 15
 2 21 20 14  8
25 19 13  7  1
18 12  6  5 24
11 10  4 23 17



My Question:
Since there is concrete rules it can be coded / programmed in any computer language.
Can some one code these rules.
Although sql server is preferred, u may code in any known language.
I can always put those code to SQL Server area of EE and can have equivalent.







Avatar of ozo
ozo
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mateen
Mateen

ASKER

Hi ozo

Would u please tell the language of the above code.
Thanks.
Perl
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mateen

ASKER

Hi ozo

With the link provided by u Now I know how to fill even square. Thanks.

Your code and that of Naishal must have been correct.

Right now I am waiting if someone can give me in sql server,
Otherwise I would ask in EE SQL Server Area to give equivalent of your codes.

Thanks again


Avatar of Mateen

ASKER

When u r extremely free, could u please send executable version (.exe file) of your code
at
imongm@gmail.com

Thanks