This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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

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.

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 ]

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

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.