Question

Formula to fill any magic square.

Asked by: Mateen

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.







This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-06-28 at 22:00:13ID21474365
Tags

magic

,

square

,

formula

Topic

Puzzles & Riddles

Participating Experts
2
Points
500
Comments
8

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Copy formula vertically, source data runs horizontally
    Another Excel question... Linking data between two spreasheets... The summary data runs vertically, but the data we're copying from runs horizontally. Again, excel won't copy our formulas, and we have too many to build by hand. Here are some sample formulas: Cell C190: =...
  2. paste formula from vertical into horizontal
    Hi, my Excel file have 2 sheets. At Sheet 1, there is a column of data with formulas. I want to copy that column and paste special as link into Sheet 2 but I need the data to be displayed horizontally. Can excel do this?
  3. Linking Vertical Data to a Horizontal table.
    I have an Excel workbook with about 130 pieces of data in a column for easy graphing. I would like to link this to a table that has a horizontal format. I can put in = and then click on the first cell, but when I fill right on the horizontal worksheet it rolls the column le...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ozoPosted on 2005-06-28 at 23:00:11ID: 14325614

 

by: ozoPosted on 2005-06-28 at 23:32:15ID: 14325716

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";
}

 

by: MateenPosted on 2005-06-29 at 00:07:02ID: 14325832

Hi ozo

Would u please tell the language of the above code.
Thanks.

 

by: ozoPosted on 2005-06-29 at 00:56:09ID: 14326000

Perl

 

by: NaishalPosted on 2005-06-29 at 02:10:27ID: 14326632

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.

 

by: ozoPosted on 2005-06-29 at 03:46:09ID: 14327175

#!/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($_));
}

 

by: MateenPosted on 2005-06-29 at 04:17:33ID: 14327328

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


 

by: MateenPosted on 2005-06-29 at 23:32:08ID: 14335832

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

Thanks

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...