# Generating Magic Squares

Page 1 of 1

## 6 Replies - 15854 Views - Last Post: 21 February 2013 - 10:57 PM

### #1 Skydiver

• Code herder

Reputation: 4839
• Posts: 15,978
• Joined: 05-May 12

# Generating Magic Squares

Posted 29 September 2012 - 03:16 PM

I figured it was time for another C# challenge. The objective is to generate normal magic squares. A normal magic square is a NxN matrix filled with all the numbers from 1..N2 and arranged such that the sum of each row, and the sum of each column is some constant number.

Here are some magic squares:
```4 9 2
3 5 7
8 1 6

7 12  1 14
2 13  8 11
16  3 10  5
9  6 15  4

```

Input:
From the command line, a number N which is the size of the square.

Output:
To the console, a grid like shown above with the number arranged in a magic square.

Language:
C# preferred, but if you need to exercise your skills in another language, feel free to post.

For initial entries you only need to support odd values for N, just print out an message saying that you don't support even sizes. For those who want to be challenged support powers of 2 for N. For bragging rights, support all even numbers.

Be sure to post your code within spoiler tags to keep this challenge fresh for everyone.

This post has been edited by Skydiver: 29 September 2012 - 03:22 PM

Is This A Good Question/Topic? 0

## Replies To: Generating Magic Squares

### #2 CasiOo

• D.I.C Lover

Reputation: 1524
• Posts: 3,401
• Joined: 05-April 11

## Re: Generating Magic Squares

Posted 30 September 2012 - 07:19 AM

Let me start with a "randomize until magic square" solution

Can only get better from here!

Works well with n == 3 or 4
Spoiler

Was This Post Helpful? 0

### #3 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Generating Magic Squares

Posted 30 September 2012 - 09:22 AM

Short method that does odd squares using a simple pattern. Still working on even squares

Spoiler

Was This Post Helpful? 0

### #4 Skydiver

• Code herder

Reputation: 4839
• Posts: 15,978
• Joined: 05-May 12

## Re: Generating Magic Squares

Posted 30 September 2012 - 11:13 AM

Momerath that's nice, short and sweet. You may want to deal with the degenerate case where n == 1, though.
Was This Post Helpful? 0

### #5 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Generating Magic Squares

Posted 30 September 2012 - 01:02 PM

Yeah, the outside code (that picks the even/odd routine) handles 1, didn't think about it here
Was This Post Helpful? 0

### #6 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Generating Magic Squares

Posted 30 September 2012 - 09:00 PM

The idea is to make the sum as 'n [ ( n^2 + 1) / 2 ]' , in a [n*n] matix

say for eg. [3*3] matrix , n=3

so sum = 3[ (3^2 +1)/2]
= 3[ 10/2]
= 3*5
=15

regards,
Raghav
Was This Post Helpful? 0

### #7 psychopyro202

• New D.I.C Head

Reputation: 1
• Posts: 1
• Joined: 21-February 13

## Re: Generating Magic Squares

Posted 21 February 2013 - 10:57 PM

I figured I would shed a little bit of light on this if anyone still wants to undergo this challenge.
Look here
Was This Post Helpful? 1

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}