Formula to fill any magic square.

Mateen
Mateen used Ask the Experts™
on
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.







Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Most Valuable Expert 2014
Top Expert 2015
Commented:
sub oddmagic{
    my $order=shift;
    if( $order%2 != 1 ){ return warn "$order not odd\n" }
    my @ordinary=@{(shift)};
    my @break=@{(shift)};
    my @x=(0,($order-1)/2);
    my @square;
    for( 1..$order*$order ){
        if( $square[$x[0]%$order][$x[1]%$order] ){
            $x[0] += $break[0]-$ordinary[0];
            $x[1] += $break[1]-$ordinary[1];
        }
        $square[$x[0]%$order][$x[1]%$order] = $_;
        $x[0] += $ordinary[0]; $x[1] += $ordinary[1];
    }
    return @square;  
}


my @s = oddmagic(7,[-1,1],[1,0]);
for( @s ){
    printf"%3d"x@$_,@$_;
    print"\n";
}

Author

Commented:
Hi ozo

Would u please tell the language of the above code.
Thanks.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Perl
Commented:
Hi Mateen,

Below isthe code which embeds the logic of the game. With little modification it will immediately run in C# or C++.  It's modularized and well structured. Have a look at it.

////////////////////////////////Code for game/////////////////
int dimension;
int StartP;
Point CurrentP=StartP;
int i=1;

do
{
      
      PrintI(CurrentP,i);
      CurrentP=GetNext(CurrentP);
      i++;

}while(i<=dimension*dimension)

point GetNext(point CurrentP)
{
      
      CurrentP.x=IncrementX(CurrentP.x);
      CurrentP.Y=IncrementY(CurrentP.Y);
      return CurrentP;
}

int IncrementX(int i)
{
      i++;
      if(i>dimension)
      {
            i=0;
      }
      return i;
}

int IncrementY(int i)
{
      i--;
      if(i<0)
      {
            i=dimension;
      }
      return i;
}

Naishal.
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/usr/bin/perl
#another constuction of odd and even squares
use strict;
use warnings;
sub oddLoubere{
    my $order=shift;
    if( $order%2 != 1 ){ return warn "$order not odd\n" }
    my @ordinary=@{(shift)};
    my @break=@{(shift)};
    my @x=(0,($order-1)/2);
    my @magic;
    for( 1..$order*$order ){
        if( $magic[$x[0]%$order][$x[1]%$order] ){
            $x[0] += $break[0]-$ordinary[0];
            $x[1] += $break[1]-$ordinary[1];
        }
        $magic[$x[0]%$order][$x[1]%$order] = $_;
        $x[0] += $ordinary[0]; $x[1] += $ordinary[1];
    }
    return @magic;  
}

sub oddConway{
    my $order=shift;
    if( $order%2 != 1 ){ return warn "$order not odd\n" }
    my @x=(0,($order-1)/2);
    my @magic;
    my @even;
    for my $n ( 1..$order*$order ){
        if( $n%2 ){
            $magic[$x[0]][$x[1]] = $n;
        }else{
            push @even,$n;
        }
        $_ += 0.5 for @x;
        if( $x[1]+$x[0] > ($order-1)*3/2 ){
            $_ += ($n%2)/2 for @x;
            while( @even ){
                $_ %= $order for @x;  
                $magic[$x[0]][$x[1]] = shift @even;
                $_ += 1 for @x;
            }
            $_ %= $order for @x;
            $_ += ($n%2)/2 for @x;
            $x[1] -= 1;
       }
    }
    return @magic;
}

sub even4{
    my $n=shift;
    return warn "$n not a multiple of 4\n" if $n%4;
    my @magic;
    for( 1..$n*$n ){
        my($r,$c) = (int(($_-1)/$n),($_-1)%$n);
        $magic[$r][$c] = ($r+$c)%8==3||($r-$c)%8==0?$n*$n+1-$_:$_;
    }
    return @magic;
}

sub even{
    my $n=shift;
    return warn "$n not even\n" if $n%2;
    return warn "no magic square of order 2\n" if $n<4;
    return even4($n) if $n%4 == 0;
    my @magic=(
    (map{[($n*$n*3/4)x($n/4),(0)x($n/4+1),($n*$n/2)x($n/4+2),($n*$n/4)x($n/4-1)]}1..$n/2),
    (map{[(0)x($n/4),($n*$n*3/4)x($n/4+1),($n*$n/4)x($n/4+2),($n*$n/2)x($n/4-1)]}1..$n/2),
    );
    @{$magic[$n/4]}[0,$n/4] = @{$magic[$n/4]}[$n/4,0];
    @{$magic[3*$n/4]}[0,$n/4] = @{$magic[3*$n/4]}[$n/4,0];
    my @s = oddLoubere($n/2,[2,1],[1,0]);
    for my $r (0..$n-1){ for my $c (0..$n-1){
        $magic[$r][$c] += $s[$r%($n/2)][$c%($n/2)];
    }}
    return @magic;
}

sub printsquare{
  my $w = length(@_**2)+1;
  for( @_ ){
    printf"%${w}d"x@$_,@$_;
    print"\n";
  }
  print "\n";
}

for( 3,5,7 ){
    printsquare(oddLoubere($_,[-1,1],[1,0]));
    printsquare(oddConway( $_ ));
}
for( 4,8,12 ){
    printsquare(even4($_));
}
for( 6,10,14 ){
    printsquare(even($_));
}

Author

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


Author

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

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial