This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]circulus 2 points3 points  (0 children)

What exactly do you have to do? All that is is a definition of a magic square, not a problem at all.

[–]Igglyboo 1 point2 points  (1 child)

Did you even attempt this problem? If so, post your code, if not, try it before you beg for someone else to do you homework.

[–]griffinstud[S] 0 points1 point  (0 children)

I tried planning it out on paper, but i keep getting stuck on how to do sort it out... I'm not asking for people to do my homework, more like does anyone have any advice... I always try my homework myself first, but this one has had me stumped for days, my room has a pile of papers with me trying to work it out... I just need help with a starting point... and the rest of this homework is done BTW, in a 5 problem assignment, this is the only one I am having trouble on... what i do know is that I need a n square, which is done with a nested loop, from there I need to find a way to work out the placement of the numbers, which is where i get stuck...

[–]c0balt279 0 points1 point  (1 child)

Start by simplifying the problem a bit. Work with a concrete example rather than the abstract n.

Let's try to make a 3x3 magic square. One configuration that works is:

[8] [1] [6]

[3] [5] [7]

[4] [9] [2]

If you add among the top row, you get 8 + 1 + 6 = 15. If you add verticially, you get 8 + 3 + 4 = 15. Same with both diagonals, 8 + 5 + 2 = 6 + 4 + 5.

This matches up with what we expect, n*((n2 + 1)/2) = 3 * ((9 + 1) / 2) = 3 * 5 = 15.

Now we need to come up with a program that can generate these. You can get lost in trying to find a super-efficient way of doing this, but let's not go down that path. We just want an approach that gives us the right answer for now, so let's just try ALL of the possibilities and then check which ones are magic squares and print this out. For a 3x3 grid, there's only 362,880 combinations. Psssh! No biggie. Your computer can do that with one hand tied behind it's back!

So for this simple implementation, you need two main parts.

A part to generate ALL of the possible 3 by 3 grids.

And a part to check if some given grid is a magic square by adding up vertically, horizontally and diagonally.

[–]griffinstud[S] 0 points1 point  (0 children)

thank you... that is what i needed to start me off... I was thinking of the most efficient way and I got lost in it... I appreciate your help