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

#1 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,986
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • 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 :D

Can only get better from here!

Works well with n == 3 or 4 :D
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • 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  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,986
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • 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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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