• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Magic Squares

 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is anyone familiar with Magic Squares? To quote from the previous link:
A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an nxn matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. It is not hard to show that this sum must be n(n^2+1)/2.

So given that definition, a 3x3 magic square would be:
8 1 6
3 5 7
4 9 2
Notice that the sum of each row, column, and the two main diagonals is equal to 3(3^2+1)/2 = 15.
The challenge is to write a small program that can produce a magic square for a given input n.
[ June 01, 2003: Message edited by: Jason Menard ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible for a 2x2 is it?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think the smallest is for a 3x3
I am trying to think this problem out, not as easy as the 9 problem...lol
I am blank...
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically the smallest would be a 1x1. But Michael is correct in that a 2x2 isn't really possible with this definition of a magic square.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I give out a magic square problem every year, usually in my C and C++ classes. Years ago I found a book on number puzzles written in Pascal. One of those puzzles was on magic squares and how to create them as well as finding the magic number.
There is a technique- to create odd number magic squares. This is a relatively straight forward. There is a way to create even magic square- with algorithm- but you cannot create a magic square for any even number- just odd ones.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is anyone familiar with Magic Squares?
yes,here
But just play it on paper with my brother who teach me how to play.
I can not remember the square is 7*7,8*8 or 9*9,10*10
But I think it is difficult to use programme to do it.
Are there any presents if anyone can write it out?
[ June 01, 2003: Message edited by: siu chung man ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for n = 4,
(0,0) (1,0) (2,0) (3,0)
(0,1) (1,1) (2,1) (3,1)
(0,2) (1,2) (2,2) (3,2)
(0,3) (1,3) (2,3) (3,3)

(0,0) + (3,0) + (3,3) + (0,3) = (1,1) + (2,1) + (1,2) + (2,2)
(0,0) + (1,1) + (2,2) + (3,3) = (0,3) + (1,2) + (2,1) + (3,0)
||
\/
(0,0) + (3,3) = (1,2) + (2,1)
 
Arjun Shastry
Ranch Hand
Posts: 1898
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Capablanca Kepler:
I did this program few months back.i will post as soon as I get it.My approach was:
1)Write the first number in the first row in the middle.
2)Move straight down and one right.
3)Move diagonally right up as possible.
4)If you are in first row go to 2.
5)If its a last column,move one row up,first element and goto step 3.
6)If its not the last column,move one down,and goto step 3.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Capablanca Kepler:
I did this program few months back.i will post as soon as I get it.My approach was:
1)Write the first number in the first row in the middle.
2)Move straight down and one right.
3)Move diagonally right up as possible.
4)If you are in first row go to 2.
5)If its a last column,move one row up,first element and goto step 3.
6)If its not the last column,move one down,and goto step 3.

But that only works for magic squares where n is an odd number. The same algorithm won't work if n is even.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are two ways to create magic squares. The way that was mentioned works for odd even magic squares use a sightly different formula.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I dug up some old papers.
Odd cell MS- 3, 5, 7, 9 -diagonal arrow method- De la Loubere procedure
Even cell MS- 4, 8, 12, 16- cross-diagonal method
 
Richard Hernandez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know how to write a simple 3*3 or 5*5 magic square with the number starting as odd?
I am new to java this year,and this one[code] is bugging me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic