# Magic Squares

Jason Menard

Sheriff

Posts: 6450

posted 12 years ago

Is anyone familiar with Magic Squares? To quote from the previous link:

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

[ June 01, 2003: Message edited by: Jason Menard ]

A magic square is an arrangement of the numbers from1ton^2(n-squared) in annxnmatrix, 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 ben(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

Eric Pascarello

author

Rancher

Rancher

Posts: 15385

6

Jason Menard

Sheriff

Posts: 6450

James Chegwidden

Author

Ranch Hand

Ranch Hand

Posts: 201

posted 12 years ago

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.

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.

Author and Instructor, my book

Francis Siu

Ranch Hand

Posts: 867

posted 12 years ago

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 ]

**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

Posts: 2545

Arjun Shastry

Ranch Hand

Posts: 1898

1

posted 12 years ago

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

Posts: 6450

posted 12 years ago

But that only works for magic squares where

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

Ranch Hand

Posts: 201

posted 12 years ago

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.

Author and Instructor, my book

James Chegwidden

Author

Ranch Hand

Ranch Hand

Posts: 201

posted 12 years ago

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

Odd cell MS- 3, 5, 7, 9 -diagonal arrow method- De la Loubere procedure

Even cell MS- 4, 8, 12, 16- cross-diagonal method

Author and Instructor, my book

Richard Hernandez

Greenhorn

Posts: 7

posted 12 years ago

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.

I am new to java this year,and this one[code] is bugging me.