• + 50 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:

    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.

    • + 2 comments

      IDK, what kind of mathematical genius human needs to be to solve this * challange....

      • + 1 comment

        Goddamn! I am already poor in maths, I noticed only 5, and not the other numbers around it, successfully wasted 20 minutes on this.

        • + 4 comments

          3 hours 😢

          • + 0 comments

            5 hours :')

          • + 0 comments

            6 hours

          • + 1 comment

            2 years

            • + 2 comments

              my lifetime

              • + 0 comments

                bruh

              • + 0 comments

                You know what they say about 10000 hours...

          • + 0 comments

            1 day :(

      • + 0 comments

        my head already started aching on the first sight.

    • + 0 comments

      Genius!!

    • + 4 comments

      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]

      • + 1 comment

        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.

        • + 1 comment

          sum of one diagonal is not 15.

          • + 2 comments

            I'm sorry, but how do you know it needs to be 15?

            • + 0 comments

              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.

            • + 0 comments

              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.

      • + 20 comments

        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.

        Mirror images:

        4 9 2 | 2 9 4
        3 5 7 | 7 5 3
        8 1 6 | 6 1 8
        -------------
        8 1 6 | 6 1 8
        3 5 7 | 7 5 3
        4 9 2 | 2 9 4
        

        Rotations:

        8 3 4 | 4 3 8
        1 5 9 | 9 5 1
        6 7 2 | 2 7 6
        -------------
        6 7 2 | 2 7 6
        1 5 9 | 9 5 1
        8 3 4 | 4 3 8
        
        • + 0 comments

          It helps me a lot understand the structure of magic square.

        • + 0 comments

          great explanations !!!

        • + 4 comments

          This explanation is beautiful!

          Much better than my suggestion that "pen and paper" shows that the middle must be 5.

          • + 0 comments

            this video explains it much better!

            https : //youtu.be/zPnN046OM34

          • + 0 comments

            your explanation was much better than anyone which helped me to understand the logic

          • + 0 comments

            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!

          • + 1 comment

            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.

            • + 1 comment

              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.

              • + 0 comments

                yes you are right @preetisakta

        • + 1 comment

          very well explained .Thanks a lot.

          • + 0 comments

            so much useful to understand the magic square.... remarkable

        • + 0 comments

          very nyc explanation!!!

        • + 0 comments

          Thank you for the clarification

        • + 0 comments

          Nicely explained man

        • + 0 comments

          woah helped me alot to solve this problem

        • + 0 comments

          👏👏👏

        • + 0 comments

          Nice explanation!!

        • + 0 comments

          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.

        • + 0 comments

          WoW, genius

        • + 0 comments

          Thanks It helps a lot

        • + 0 comments

          thanks greatly explained! but can we code it?

        • + 0 comments

          Great explanation. Thank you.

        • + 0 comments

          It helped a lot!

        • + 0 comments

          Thanks a lot , made me easily imagining the solution

        • + 0 comments

          Great Knowledge and interpretation skill. Thanks!!!

        • + 0 comments

          Thank you very much!

        • + 0 comments

          Very clever, thanks @Lachy

      • + 0 comments

        For me it was matrices like yours here that made me realize why the middle had to be 5.

    • + 2 comments

      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.

      • + 0 comments

        just simply awesome...

      • + 0 comments

        The middle number has to be 5. You can get a hint if you have read centroid decomposition.

    • + 0 comments

      Perhaps you could elaborate whats with bit pen/paper.

    • + 0 comments

      This guy.......... is Captain Osum!

    • + 0 comments

      Wow! Toppest problem solving technique. I'm trying to understand it though

    • + 0 comments

      Great logic. I would really like to know how to came up with such a solution.

    • + 0 comments

      super bro..

    • + 0 comments

      you sir, are a genius

    • + 1 comment

      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

      • + 1 comment

        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,

        1. There is absolutely no need of calculating the entire sum to prove.
        2. 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.
        • + 0 comments

          Didn't think to add the plus 1 to the (n * n) value! Brilliant. Thanks :)

    • + 0 comments

      Your observations make things easier, great!

    • + 0 comments

      Now this solution I like

    • + 0 comments

      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.

    • + 0 comments

      AWESOME !

      Mathematical GENIUS

    • + 0 comments

      your explanation is too cool , bro..

    • + 0 comments

      Absolutely

    • + 0 comments

      Genius

    • + 0 comments

      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.

      def formingMagicSquare(s):
          cost = abs(s[1][1] - 5)
          costArr = [0, 0, 0, 0, 0, 0, 0, 0]
          pickArr = [0, 2, 6, 8]
          for i in range(4):
              n = pickArr[i]
              m = pickArr[i+1] if i % 2 == 0 else pickArr[i-1]
              p = (n+m) / 2
              q = (8-m+n) / 2
              costArr[i*2] += abs(s[n/3][n%3] - 4)
              costArr[i*2] += abs(s[(8-n)/3][(8-n)%3] - 6)
              costArr[i*2] += abs(s[m/3][m%3] - 2)
              costArr[i*2] += abs(s[(8-m)/3][(8-m)%3] - 8)
              costArr[i*2] += abs(s[p/3][p%3] - 9)
              costArr[i*2] += abs(s[(8-p)/3][(8-p)%3] - 1)
              costArr[i*2] += abs(s[q/3][q%3] - 3)
              costArr[i*2] += abs(s[(8-q)/3][(8-q)%3] - 7)
              costArr[i*2+1] += abs(s[n/3][n%3] - 4)
              costArr[i*2+1] += abs(s[(8-n)/3][(8-n)%3] - 6)
              costArr[i*2+1] += abs(s[m/3][m%3] - 8)
              costArr[i*2+1] += abs(s[(8-m)/3][(8-m)%3] - 2)
              costArr[i*2+1] += abs(s[p/3][p%3] - 3)
              costArr[i*2+1] += abs(s[(8-p)/3][(8-p)%3] - 7)
              costArr[i*2+1] += abs(s[q/3][q%3] - 9)
              costArr[i*2+1] += abs(s[(8-q)/3][(8-q)%3] - 1)
          return min(costArr) + cost
      
    • + 0 comments

      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?

    • + 0 comments

      so smart!

    • + 0 comments

      Superb bro!..

    • + 0 comments

      Wow!! This is really an awesome solution. Very interesting observations to solve this problem.

    • + 0 comments

      i am bowing.

    • + 0 comments

      I have tried to explain this in a video https://youtu.be/eVHCePHyE6M

    • + 0 comments

      Atleast now I can understand the problem and work towards solution

    • + 1 comment

      Hope I can be as smart as you.

      • + 0 comments

        Thanks for the compliment... i believe a smarter person is inside you... keep practicing and improving yourself

    • + 0 comments

      GOD level

    • + 0 comments

      AWESOME

    • + 0 comments

      i'm wondering, why the center of matrix must be 5?

    • + 0 comments

      how did you come up with that center must be 5 ?

    • + 0 comments

      Mind blown

    • + 0 comments

      that helped a lot buddy

    • + 0 comments

      hyper talented:))

    • + 0 comments

      After reading this statement i got a clear view of this question thanks alot dude

    • + 0 comments

      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

    • + 0 comments

      You are the most awesome one

    • + 0 comments

      hinting about pre computing in the solutions while pre computing much deeper for the solution.

    • + 0 comments

      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

    • + 0 comments

      An impressive answer! Could you tell me what all concepts did you use to solve this question?

    • + 0 comments

      I proved the same things with Math on pen and Paper but I lack on the coding part. :|

    • + 0 comments

      XD genius. Thank you

    • + 0 comments

      You are a genius!

    • + 0 comments

      Genuis

    • + 0 comments

      damn lots of paper work u did to solve this problem :D amazing :D

    • + 0 comments

      Exceptional thinking and observation !!!

    • + 0 comments

      Why didn't I notice it !! Now it will be solved easily .