We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
All of the posted solutions require pre-computing all eight magic squares. I wanted to offer a few suggestions on how to generate them -- or at least show what I did.
We can start with two observations:
a) the "middle" of any 3x3 magic square must be 5, and
b) the "magic sum" must be 15.
Here's a way to think about the "magic sum". The sum of numbers 1-9 is 45. The three horizontal rows will include all 9 numbers (and thus sum to 45). And since there are three rows, each row will sum to 45/3 = 15.
It takes a bit of pen and paper to see that the middle must be 5 (or at least, it took me some trial and error). But once we know these two things, we can think in terms of the 4 "pairs" that can go on opposite sides of the 5:
1 and 9
2 and 8
3 and 7
4 and 6
So for example, if 4 goes Top/Left, we know that 6 must go Bottom/Right (since the "magic sum" must be 15, and 5 is in the middle)
A bit more on pen/paper will show that only two of these pairs fit in the "corners":
2 and 8
4 and 6
The other two pairs must be "wedged" inside the corner paris (e.g., top middle, bottom middle). And once we set our four corners, there is only 1 way to place the rest of the numbers.
This is enough to show that there are 8 magic matrices. There are 4 possible ways to place the 4 and 6 pair (the 4 could go in Top/Left, Top/Right, Bottom/Right, Bottom/Left). Then once we place the 4 and 6, there are two different ways we could place the 2 and 8.
To acutally generate these matrices, I started with one "seed" (which happend to be the first magic matrix I found):
[4 3 8]
[9 5 1]
[2 7 6]
From ths seed, we can rotate it clockwise 4 times (so the 4 appears in each corner). And then from each rotation, we can place the remaining digits either clockwise, or counterclockwise around the 5 (going counterclockwise is equivalent to getting the "mirror" of the matrix along a diagonal).
These two matrix manipulations are also great helper functions to have handy on other problems.
The 8 valid combinations of 3 numbers that add to 15 are:
9 5 1
7 5 3
2 5 8
4 5 6
2 9 4
6 1 8
6 7 2
8 3 4
All 8 of those combinations need to appear in the square as a row, column or diagonal. The centre cell must appear in the middle row, middle column and both diagonals. So it must be a number that appears four times, and the only digit that does is 5. So 5 must be the centre.
Similarly, each of the corner pieces must form part of a row, a column and a diagonal. So each corner cell must be a number that appears 3 times. Those are the even numbers 2, 4, 6 and 8. That means the diagonals must be "2 5 8" and "4 5 6"
2 _ 4
_ 5 _
6 _ 8
That leaves the middle edge cells, each of which needs to appear in a row and a column. These are the odd numbers 1, 3, 7 and 9. So the middle row and column must be "9 5 1" and "7 5 3". Just insert these into the above such that the rows and columns add up correctly, leaving you with:
2 9 4
7 5 3
6 1 8
From that you can take the mirror images (horizontally and vertically) and the rotation of each.
Kevinagin and Lachy, Your explanations are really wonderful! I started to code the solution and got lost. I was going to use an awful backtracking approach! But, instead of just yet another coding exercise, it (the process of understanding this) became much more interesting and elegant. Thank you!
Would like to ask if [[2,2,2],[2,2,2],[2,2,2]] is a magic square? Because the question is about the row, column or diagonals when summed up should be same "the magic constant" and not necessarily 15.
Yes, its true
I also reached the same conclusion as yours
THe middle no. always gonna be 5
The corners will always be even nos. and the diagonal counterpart of each corner will 10-x(x: the respective even nol.)
Similarly it goes for the remaining middle elements.
[4 3 8]
[9 5 1]
[2 7 6]
One can start a basic matrix like this and they can rotate it 4 times to get a matrix with corner have different even no.
The other 4 can be achieved by taking their respective transpose.
But to culminate all of this coding requires one to have a better hold on matrix loops cause rotating matrix is a tedious job. Hence, it would be better to have a predefined set of 8 matrices.
Surely this problem does not come under easy section.
Obviously, that is why the sum of AP is N*( a+l)/2. it is always the middle term of AP that balances the entire series.
For this case middle term is : (a+l) / 2 = (1+9) /2 =5 .
Where a : first term of the series and l : last term of the series
and as a consecuence the sum will have to be : n * 5 = 9 * 5 = 45
Therefore,
There is absolutely no need of calculating the entire sum to prove.
If the dimension is not odd, then there is no middle element, but for that the near middle elements will be near to median of the series.
I found this to be well explained, however adding to how i found that 5 should appear in the middle, since we have aleady established each row, column and diagnol must be of sum 15 we have 3 values who can't appear together in a row column or diagnol namely, 7,8,9 since each of them will lead to the sum being greater than 15. Now, 6 can't appear with 9. Using this and filling the matrix, we establish that 8 should appear in the corner position only, with the value of middle being 5, from there we can proceed with the information above.
Nice explanation. I used this logic and wrote the python code as under. Instead of constructing all magic squares, I found out cost for all magic squares and took minimum.
I don't know why it tooked pen and paper, to you, for me after reading the question i came to know rotating and swapping will give all possible ways....but i stucked at implementing. so pls if you can help how to implement?
let centre be x
sum of all numbers in the magic square =(9+8+...+1)=45
as this is sum of 3 rows, row sum =45/3=15 which equals column sums and diagonal sums
sum of all numbers except the centre =45-x
now the centre appears in four sums(middle row, middle column, and the diagonals):
sum of all these sums=15*4=60 in which x is added 4 times
so,
sum of all numbers except the centre =45-x=60-4x
which means x=5
for those who dun understand why 2/4/6/8 should be placed in the corner
the reason is the number parity.
after we know the middle must be 5, there are 8 numbers left.
____5____
(i will simply use 'row' to represent row/ column/ diagonal, since same logic applies to all kind)
for the middle row, 2 numbers add to 10, it could be any combination.
but for the first and third row, there are 2 combinations to sum to 15. either 2 even numbers + 1 odd number OR 3 odd numbers. we know that 1/9 and 3/7 should be used in pair, we cannot use the whole pair plus one of another pair in a row. (and actually it cannot sum to 15 as well) therefore, we know that we can only use 1 odd number in row/ column 1/ 3, and the even numbers will be located at the corner
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
All of the posted solutions require pre-computing all eight magic squares. I wanted to offer a few suggestions on how to generate them -- or at least show what I did.
We can start with two observations: a) the "middle" of any 3x3 magic square must be 5, and b) the "magic sum" must be 15.
Here's a way to think about the "magic sum". The sum of numbers 1-9 is 45. The three horizontal rows will include all 9 numbers (and thus sum to 45). And since there are three rows, each row will sum to 45/3 = 15.
It takes a bit of pen and paper to see that the middle must be 5 (or at least, it took me some trial and error). But once we know these two things, we can think in terms of the 4 "pairs" that can go on opposite sides of the 5:
So for example, if 4 goes Top/Left, we know that 6 must go Bottom/Right (since the "magic sum" must be 15, and 5 is in the middle)
A bit more on pen/paper will show that only two of these pairs fit in the "corners":
The other two pairs must be "wedged" inside the corner paris (e.g., top middle, bottom middle). And once we set our four corners, there is only 1 way to place the rest of the numbers.
This is enough to show that there are 8 magic matrices. There are 4 possible ways to place the 4 and 6 pair (the 4 could go in Top/Left, Top/Right, Bottom/Right, Bottom/Left). Then once we place the 4 and 6, there are two different ways we could place the 2 and 8.
To acutally generate these matrices, I started with one "seed" (which happend to be the first magic matrix I found):
From ths seed, we can rotate it clockwise 4 times (so the 4 appears in each corner). And then from each rotation, we can place the remaining digits either clockwise, or counterclockwise around the 5 (going counterclockwise is equivalent to getting the "mirror" of the matrix along a diagonal).
These two matrix manipulations are also great helper functions to have handy on other problems.
IDK, what kind of mathematical genius human needs to be to solve this * challange....
Goddamn! I am already poor in maths, I noticed only 5, and not the other numbers around it, successfully wasted 20 minutes on this.
3 hours 😢
5 hours :')
6 hours
2 years
my lifetime
bruh
You know what they say about 10000 hours...
1 day :(
my head already started aching on the first sight.
Genius!!
Didn't get the part which said the center must be "5".
How about this following matrix, it satisfies all the condition:
[3 4 8] [6 7 2] [9 1 5]
or this: [6 7 2] [3 4 8] [9 1 5]
Not sure how you read this matrix, but if it is
3 4 8
6 7 2
9 1 5
I dont see it satisfies the conditions.
Or if it is read like this :
3 6 9
4 7 1
8 2 5
I still dont see this satisfies any condition.
sum of one diagonal is not 15.
I'm sorry, but how do you know it needs to be 15?
By observing. There are just 8 magic square matrix available with 3x3 matrix. And all of them has row/col/diagonal sum of 15. check it out.
matrix should contain distinct positive integers. 3x3 matrix contains 9 items, so sum of (1 - 9) is 45. if you divide it by 3, each row sum is 15.
The 8 valid combinations of 3 numbers that add to 15 are:
All 8 of those combinations need to appear in the square as a row, column or diagonal. The centre cell must appear in the middle row, middle column and both diagonals. So it must be a number that appears four times, and the only digit that does is 5. So 5 must be the centre.
Similarly, each of the corner pieces must form part of a row, a column and a diagonal. So each corner cell must be a number that appears 3 times. Those are the even numbers 2, 4, 6 and 8. That means the diagonals must be "2 5 8" and "4 5 6"
That leaves the middle edge cells, each of which needs to appear in a row and a column. These are the odd numbers 1, 3, 7 and 9. So the middle row and column must be "9 5 1" and "7 5 3". Just insert these into the above such that the rows and columns add up correctly, leaving you with:
From that you can take the mirror images (horizontally and vertically) and the rotation of each.
Mirror images:
Rotations:
It helps me a lot understand the structure of magic square.
great explanations !!!
This explanation is beautiful!
Much better than my suggestion that "pen and paper" shows that the middle must be 5.
this video explains it much better!
https : //youtu.be/zPnN046OM34
your explanation was much better than anyone which helped me to understand the logic
Kevinagin and Lachy, Your explanations are really wonderful! I started to code the solution and got lost. I was going to use an awful backtracking approach! But, instead of just yet another coding exercise, it (the process of understanding this) became much more interesting and elegant. Thank you!
Would like to ask if [[2,2,2],[2,2,2],[2,2,2]] is a magic square? Because the question is about the row, column or diagonals when summed up should be same "the magic constant" and not necessarily 15.
In a magic square each number should occur only once. In this example , you are using 2 multiple times. So, it cannot be a magic sqaure.
yes you are right @preetisakta
very well explained .Thanks a lot.
so much useful to understand the magic square.... remarkable
very nyc explanation!!!
Thank you for the clarification
Nicely explained man
woah helped me alot to solve this problem
👏👏👏
Nice explanation!!
This is the only way the solution is understandable after you get the fact that the middle must be 5 and the 4,2,6,8 must be in the corners.
WoW, genius
Thanks It helps a lot
thanks greatly explained! but can we code it?
Great explanation. Thank you.
It helped a lot!
Thanks a lot , made me easily imagining the solution
Great Knowledge and interpretation skill. Thanks!!!
Thank you very much!
Very clever, thanks @Lachy
For me it was matrices like yours here that made me realize why the middle had to be 5.
Yes, its true I also reached the same conclusion as yours THe middle no. always gonna be 5 The corners will always be even nos. and the diagonal counterpart of each corner will 10-x(x: the respective even nol.) Similarly it goes for the remaining middle elements. [4 3 8] [9 5 1] [2 7 6] One can start a basic matrix like this and they can rotate it 4 times to get a matrix with corner have different even no. The other 4 can be achieved by taking their respective transpose. But to culminate all of this coding requires one to have a better hold on matrix loops cause rotating matrix is a tedious job. Hence, it would be better to have a predefined set of 8 matrices. Surely this problem does not come under easy section.
just simply awesome...
The middle number has to be 5. You can get a hint if you have read centroid decomposition.
Perhaps you could elaborate whats with bit pen/paper.
This guy.......... is Captain Osum!
Wow! Toppest problem solving technique. I'm trying to understand it though
Great logic. I would really like to know how to came up with such a solution.
super bro..
you sir, are a genius
The way you calculate the MUST center of any magic square with odd numbers (e.g. 3x3, 5x5, 7x7) = is to calculate the total sum of Magic Square.
3 by 3 = 45, 5 by 5 = 325, 7 by 7 = 1225,
And then divide sum by the total number of squares in the Magic Square. (3x3=9, 5x5=25, 7x7=49):
45/9 = 5 325/25 = 13 1225/49 = 25
Obviously, that is why the sum of AP is N*( a+l)/2. it is always the middle term of AP that balances the entire series. For this case middle term is : (a+l) / 2 = (1+9) /2 =5 . Where a : first term of the series and l : last term of the series and as a consecuence the sum will have to be : n * 5 = 9 * 5 = 45
Therefore,
Didn't think to add the plus 1 to the (n * n) value! Brilliant. Thanks :)
Your observations make things easier, great!
Now this solution I like
I found this to be well explained, however adding to how i found that
5
should appear in the middle, since we have aleady established each row, column and diagnol must be of sum15
we have 3 values who can't appear together in a row column or diagnol namely,7,8,9
since each of them will lead to the sum being greater than 15. Now,6
can't appear with9
. Using this and filling the matrix, we establish that8
should appear in the corner position only, with the value of middle being5
, from there we can proceed with the information above.AWESOME !
Mathematical GENIUS
your explanation is too cool , bro..
Absolutely
Genius
Nice explanation. I used this logic and wrote the python code as under. Instead of constructing all magic squares, I found out cost for all magic squares and took minimum.
I don't know why it tooked pen and paper, to you, for me after reading the question i came to know rotating and swapping will give all possible ways....but i stucked at implementing. so pls if you can help how to implement?
so smart!
Superb bro!..
Wow!! This is really an awesome solution. Very interesting observations to solve this problem.
i am bowing.
I have tried to explain this in a video https://youtu.be/eVHCePHyE6M
Atleast now I can understand the problem and work towards solution
Hope I can be as smart as you.
Thanks for the compliment... i believe a smarter person is inside you... keep practicing and improving yourself
GOD level
AWESOME
i'm wondering, why the center of matrix must be 5?
how did you come up with that center must be 5 ?
Mind blown
that helped a lot buddy
hyper talented:))
After reading this statement i got a clear view of this question thanks alot dude
Reason for centre to be 5:
let centre be x sum of all numbers in the magic square =(9+8+...+1)=45 as this is sum of 3 rows, row sum =45/3=15 which equals column sums and diagonal sums
sum of all numbers except the centre =45-x
now the centre appears in four sums(middle row, middle column, and the diagonals): sum of all these sums=15*4=60 in which x is added 4 times so,
sum of all numbers except the centre =45-x=60-4x which means x=5
You are the most awesome one
hinting about pre computing in the solutions while pre computing much deeper for the solution.
for those who dun understand why 2/4/6/8 should be placed in the corner
the reason is the number parity.
after we know the middle must be 5, there are 8 numbers left.
(i will simply use 'row' to represent row/ column/ diagonal, since same logic applies to all kind)
for the middle row, 2 numbers add to 10, it could be any combination.
but for the first and third row, there are 2 combinations to sum to 15. either 2 even numbers + 1 odd number OR 3 odd numbers. we know that 1/9 and 3/7 should be used in pair, we cannot use the whole pair plus one of another pair in a row. (and actually it cannot sum to 15 as well) therefore, we know that we can only use 1 odd number in row/ column 1/ 3, and the even numbers will be located at the corner
An impressive answer! Could you tell me what all concepts did you use to solve this question?
I proved the same things with Math on pen and Paper but I lack on the coding part. :|
XD genius. Thank you
You are a genius!
Genuis
damn lots of paper work u did to solve this problem :D amazing :D
Exceptional thinking and observation !!!
Why didn't I notice it !! Now it will be solved easily .