# Any ideas for improvement in this code?

I wrote a code that generates and adds all the horizontal and vertical lines of a 3X3 square to get the same number, I would love to be able to know how to code it in order for the diagonals to also be able to add up to said number, would anybody like to help me? http://www.sololearn.com/app/python/playground/cJvT8rVx32p5/

5/9/2017 4:07:01 PM

TheLegend276 Answers

New AnswerI just used OOP here, because I wanted to solve this using techniques from linear algebra and a matrix class would make that convenient. The key insight here should be, that you can choose three entries, which uniquely determine all others in a magic square. If you ignore the rest of my code and find such a set of entries, you can explicitly calculate the whole square from there. I'd recommend you try that and ask here if you get stuck. (My solution is doing the same, but written down in a fancy way.)

Please edit the link, it currently takes me to Google Play. To your question: Magic squares of size 3*3 form a 3-dimensional sub vector space of R^(3*3) and it should be possible to write down a basis b1, b2, b3, where every element has only integers as entries. With randomly generated integers c1, c2, c3, the matrix c1b1+c2b2+c3b3 is another magic square.

I took the time to write a little code: https://code.sololearn.com/ckh3hEhqR5HO If you modify the rand range or the basis elements, you will probably generate a different set of magic squares. The probability distribution will however be uniform on the set of squares you can randomly generate this way.

Pretty much. Also a little observation: My system uses two numbers less than yours and satisfies two additional constraints (the diagonal sums being the same as the row/col sums). In a system of linear equations, more constraints mean less free variables and that principle is at work here. If you have numbers a,b,c given, can you complete a ? b c ? ? ? ? ? to a magic square? And if so, is that square uniquely determined?

Oh ok, so I think I understand the difference between yours and mine, let me know if I'm wrong: In mine I made it choose between 5 semi-random entries which eventually determined the rest of the square, the first variable mine chooses is the final number everything else is going to add to, followed by a random center, two random edges and one random corner. All of which have certain parameters to make sure they add up to the final result, everything else is just the missing numbers to get to that result Yours gets a random set of 3 numbers that is determined through an algebraic function and the final result everything adds up to is determined with those 3 numbers, not before them. In that way because it wasn't 100% random and it did have an algebraic function to help determine them everything else just works with the program and not against it in order to find the other numbers which because is a linear equation are 100% guaranteed to exist.

thanks a lot. i guess i really need to practice object oriented programming more often...