26279 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Dell JUNKS public
cloud in favor of
partner tech
WordPress warns of
mass Tumblr
defections after
Yahoo! deal
Flailing QLogic"s
boss gives up CEO,
president gigs
Rogue Nokia
splinter cell drops
its Jolla phone
A-BOMB
They WANT to EAT
YOUR COMPUTER -
welcome your ANT
overlords
Canadian regulators
welcome US Bitcoin
refugees with open
arms
Securo-boffins
uncover new GLOBAL
cyber-espionage
operation
It! Started! With!
A! GIF!... Yahoo!
Actually! Buys!
Tumblr! for! $1bn!
Petshop iPad fanboi
charged with
filming up young
model"s skirt
Amazon
cloud-watcher shows
some love for
Microsoft"s Azure
Slashdot
Book Review: Locked
Down: Information
Security For
Lawyers
Dell Dumps Its
Public Cloud
Offerings
Narrowing Down When
Humans Began
Hurling Spears
What Professors Can
Learn From "Hard
Core" MOOC Students
FDA To Decide Fate
of Triclosan,
Commonly Used In
Antibacterial Soaps
Cyber Attack From
Inside India Hits
Pakistan Government
Jolla Announces
First Meego Phone
Available By End
2013
Over 100 Hours of
Video Uploaded To
YouTube Every
Minute
Interviews: McAfee
Says House Fire Was
No Accident
Yahoo Pinkie-Swears
It Won"t Ruin
Tumblr
Article viewer

The Fascinating Magic Squares



Written by:anilg
Published by:Nightscript
Published on:2006-05-10 23:41:56
Topic:Maths
Search OSI about Maths.More articles by anilg.
 viewed 27003 times send this article printer friendly

Digg this!
    Rate this article :
Its an old branch (a twig, rather) of arithmetic. Read through and be facinated by the Magic Squares!

I'd read about these a long time back. I recently found the book in question and wanted to share this beautiful twig of mathematics. Lets get the definitions right.

A Magic Square is a square matrix where the sum of all columns, rows and diagonals is constant.

Consider the following 3rd order Magic square :



The above square shows a simple 3rd order Magic Square. 8 other combinations are possible by shifting Rows/Colums Up/Right.

Odd order Magic Squares

Consider the squares below



Hmm.. interesting. Well, lets get down to the algorithm for an odd order n matrix.

Algorithm for Odd Order Magic Squares.

Top left cell = (X,Y) = (0,0)

  • Step 1 : Pick any X , Y. (I take this as the center of the first row)
  • Step 2 : N = 1
  • Step 3 : While N <= Sqr(Order)
  • Step 4 : Write N at (X,Y)
  • Step 5 : TempX = (X++) mod N
  • Step 6 : TempY = (Y--) mod N
  • Step 7 : IF (TempX,TempY) is Empty X = TempX, Y = TempY
    ELSE Y++
  • Step 8 : Loop Step 3
  • Step 9 : S = (1/2)(order)(1 + sqr(order))


Graphically shown for plotting order '3' Magic Square:




Similarly, view how the same algorithm was used in Fig.2 5th and 7th order Magic Squares

Also for odd ordered magic squares thus constructed, the sum of rows, colums, diagonals is given by S = (1/2)(order)(1 + sqr(order)).

//C++ code to generate odd-order Magic Squares
//Porting should be a POC
//(c) osix.net/~anilg
#include <iostream>
#define MAX 15
using namespace std;

int main()
{
  int order, n, X, Y, i, j, a[MAX][MAX],s;

  cout<<"Enter the order of matrix (odd) :";
  cin>>order;
  
  if(!order%2 || order>MAX) return -1; // for those who chose to be stubborn

  for(i=0;i<order;i++)
    for(j=0;j<order;j++)
      a[i][j]=0;

  X = 0;
  Y = order/2;

  for(n=1;n<=(order*order);n++)
  {
     a[X][Y] = n;
     if (a[(X+order-1)%order][(Y+order+1)%order])
       X = (X+order+1)%order ;
     else
       X = (X+order-1)%order , Y = (Y+order+1)%order ;
  }
  
  for(i=0;i<order; i++)
  {
    cout<<'n' ;
    for(j=0;j<order; cout<<a[i][j++]<<'t'); //for order>9,i'd change 't' to ' '
  }
  
  s= (1/2.0)*order*(1+order*order);
  cout<<"nnSum of Rows,Coulums,Diagonals = "<<s;

  system("pause>nul"); //getch() equivalent
                       // non-windows ppl remove this
  return 0;
}




Even Order Magic Squares

Things start getting a little difficult to automate for even-ordered Magic Squares..

4th order Magic square.

  • Step 1 : Write down the numbers 1 to 16 normally, left to right, top to bottom
  • Step 2 : Mark the Diagonals.. and swap the numbers symmetrically (oh you'll understand)..




An 8th order Magic Square is very similar to 4th order. It can be considered as consisting of 4 fourth order Squares.And you know what they say about picture and thousand words!



Now dont ask me for a 16th order MS... figure it out!

Special Magic Squares

Bhaskaracharya's Magic Square

The following magic square was constructed by the Indian :) mathematician, Bhaskaracharya. It's a 9-by-9 Magic square.



The interesting part being:

  • It uses only numbers 1 to 9.
  • The numbers in a column are in arithmetical progression (mod 10)
  • Oh... and look at all the 1's, column after column.


Multiplication Magic Square

Well, they're the same as normal magic squares; except you multiply the elements of rows, columns, diagonals.



Thats it??? Hell no!

Magic cubes

No explainations. See and understand

Order 2:



And order 3:




Conclusion

Well that's it for now folks. I'm sure you enjoyed this article.

(c) anilg

Did you like this article? There are hundreds more.

Comments:
Anonymous
2006-01-21 20:08:34
good article :) yesterday night i was playing sudoku, an addictive game similar to magic squares
jmordax
2006-01-24 11:38:23
Yeah... cool article. Very well explained.
BTW, and following Anonymous comment... are you aware of any such document explaining the creation of a SuDoKu?
anilg
2006-01-26 05:32:58
As far as i know... sudoku is about one number appearing only once in every row and column. It doesnt imply a solved sudoku is a magic square..

Incidentally Fig.6 is a legal 'solved' sudoku problem.
jmordax
2006-01-26 09:34:16
I agree. A SuDoKu and a Magic Square are two different things... but they are similar and that was the reason of the question, because I liked a lot the explanation in this article.

BTW, Fig.6 is not a correct SuDoKu. You can't have reptitions in each 3x3 squares.
anilg
2006-01-26 09:39:30
Hmm.. my local paper carries that rule (no rep in 3x3) under 'difficult' catagory.

Ur allowed to do that in the 'simple' rules
bb
2006-01-26 11:43:52
sweet. im was a suduko fan, but after doing about 500+ puzzles got a bit bored with the repetition. i've now moved onto 'killers' which are a bit more challenging
Anonymous
2006-02-10 00:22:34
kool, without the graphic explanation I would have never been able to program it...
anilg
2006-04-14 08:21:25
When r the pictures coming back up?
bb
2006-04-14 12:21:50
as soon as t12 gets them to me!
jonoringading
2006-07-07 09:43:57
I learned a different algorithm for odd order magic squares, which is easier for humans...start with 1 in the bottom middle, and go diagonally down and right, then keep going. If you get to the edge, just continue on the opposite side where you would be if the square was tiled infinitly next to itself.
if you bump into a number already filled in, jump up one and continue down and to the right again, this gives you:
4 9 2
3 5 7
8 1 6
which is a mirror of your solution, but you don't need modulus math. Great article!
cheers,
Jono
paranoia
2006-08-29 15:50:13
that method does exactly what his does except rotated and translated. I'm fairly certain that any initial location and any direction of diagonal travel combined with the appropriately rotated jump when you hit a filled square will solve the magic square. and the modulus math is what does the wrapping when you get to the edge, your solution has it too you just didn't specifically call it that.
Anonymous
2007-01-07 07:08:11
<pre>
Multiplication Magic Square
This seems better than yours.
Order 5 pandiagonal magic square

1 24 18 432 324 60466176 --- Row 1
54 1296 4 3 72 60466176 --- Row 2
12 9 216 162 16 60466176 --- Row 3
648 2 48 36 27 60466176 --- Row 4
144 108 81 8 6 60466176 --- Row 5

60466176 --- Column 1
60466176 --- Column 2
60466176 --- Column 3
60466176 --- Column 4
60466176 --- Column 5

60466176 --- Right Diagonal 1
60466176 --- Right Diagonal 2
60466176 --- Right Diagonal 3
60466176 --- Right Diagonal 4
60466176 --- Right Diagonal 5

60466176 --- Left Diagonal 1
60466176 --- Left Diagonal 2
60466176 --- Left Diagonal 3
60466176 --- Left Diagonal 4
60466176 --- Left Diagonal 5

65 --- Standard multiplication
************************************************

1 24 18 432 324
54 1296 4 3 72
12 9 216 162 16
648 2 48 36 27
144 108 81 8 6

Magic multiplication= 60466176
************************************************
mars
</pre>
Anonymous
2007-04-14 10:14:58
tis technique is very cool & sexy
Anonymous
2007-10-01 08:51:31
can yu create a program to display an even powered magic aquare?
Anonymous
2008-01-17 06:47:04
can i get the same code in c#
Anonymous
2008-03-23 08:16:17
http://www1.gantep.edu.tr/~bingul/php/magic/en-index.php
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Math Test by mattman059

Goes through some basic Boolean arithmetic and a little set theory.
Number Translation Test by crazynird

So you think you're good at translating numbers? Then try out this test!


     
Your Ad Here
 
Copyright Open Source Institute, 2006