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 ]
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15003
posted
0
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
Joined: Nov 09, 2000
Posts: 6450
posted
0
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
Joined: Oct 06, 2002
Posts: 201
posted
0
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.
Mr. C<br /> <br />Author and Instructor<br />My book:<br /><a href="http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html" target="_blank" rel="nofollow">http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html</a>
Francis Siu
Ranch Hand
Joined: Jan 04, 2003
Posts: 867
posted
0
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 ]
Francis Siu
SCJP, MCDBA
John Lee
Ranch Hand
Joined: Aug 05, 2001
Posts: 2545
posted
0
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)
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.
MH
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
posted
0
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
Joined: Oct 06, 2002
Posts: 201
posted
0
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
Joined: Oct 06, 2002
Posts: 201
posted
0
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
Joined: Feb 05, 2004
Posts: 7
posted
0
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.