3 6 2 8 8 0 ways of arranging the numbers
Out of the1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
in a 3 × 3 matrix, how many configurations are there such that all lines (rows, columns and diagonals) add up to the same sum?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
There is only 1 magic square and we can turn it on 90 degrees and reflect each by horizontal axe. So we get 8 different configuration with the same numbers.
It would help if you described what a magic square is in your answer
If all rows, columns, and diagonals add up to the same sum, that means the total sum of the 3x3 matrix must be divided equally among the rows, columns, and diagonals. Since the total sum of the matrix is always 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45, each row, column, and diagonal must have a sum of 45 / 3 = 15.
There are a fixed number of ways for each number from 1 to 9 to contribute towards a total of 15 when added with two other numbers.
We see that 5 can contribute towards the sum in four different ways. Hence, it must always be strategically placed at the center because this is the only way the middle row, the middle column, and the two diagonals can add up to 15. The reason why 5 is placed in the center is that the center is part of the two diagonals, the middle row, and the middle column. As seen above, 5 can be added up to 15 in FOUR unique ways, so each sum together will cover these FOUR paths along the two diagonals, the middle row, and the middle column.
We see that 2, 4, 6, and 8 can contribute towards the sum in three different ways. Hence, these numbers must always be strategically placed at one of the four corners so that one row, one column, and one diagonal can add up to 15. The reason why 2, 4, 6, and 8 are placed at the corners is that the corners are part of one diagonal, one row, and one column. As seen above, 2, 4, 6, and 8 can be added up to 15 in THREE unique ways, so each sum together will cover these THREE paths along the diagonal, the row, and the column.
Finally, we see that 1, 3, 7, and 9 can contribute towards the sum in two different ways. Hence, these numbers must always be strategically placed on the edges i.e. the four remaining slots other than the corners and the center so that one row and one column can add up to 15. The reason why 1, 3, 7, and 9 are placed on the edges is that the edges are part of one row and one column. As seen above, 1, 3, 7, and 9 can be added up to 15 in TWO unique ways, so each sum together will cover these TWO paths along the row and the column.
Now that we have the setup, counting the number of configurations should be relatively simple.
5 will always have to go to the center so there is only one way this can be done.
If we fix 2, 4, 6, or 8 to one of the corners, this automatically implies that the corner directly opposite is also fixed. For eg: if we pick 2 to go on one of the corners, the corner directly opposite to 2 will require an 8 so that the diagonal has 2 + 5 + 8 = 15.
There are four corners where 2 can be placed, which automatically means 8 must go to the corner opposite of wherever 2 is placed. For each of these four scenarios, this leaves us with two possible corners where we can place 4 or 6. Placing either one in either corner will automatically seal the placement of another in the opposite corner so that diagonal has 4 + 5 + 6 = 15.
Placing 5 in the center and 2, 4, 6, and 8 in the corners automatically means that the fate of the rest of the numbers - 1, 3, 7, and 9, is sealed already because they can only occupy a single position that will make the rows and columns sum to 15.
To summarize, if we proceed step-by-step in the most optimal manner,
Therefore, the total number of configurations = 1 × 4 × 2 × 1 = 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Given the computationally small space and algorithm's next permutation function, this is near-trivial in C++: #include <iostream> #include <algorithm> using namespace std; bool ismagicsquare(int []); int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; int count = 0; do { if(ismagicsquare(myints)) count++; } while (next permutation(myints,myints+9) );
cout << count << endl;
return 0;
}
bool ismagicsquare(int list[]) {
int sum = list[0] + list[1] + list[2];
if(sum == (list[3] + list[4] + list[5]) && sum == (list[6] + list[7] + list[8]) &&
sum == (list[0] + list[3] + list[6]) && sum == (list[1] + list[4] + list[7]) && sum == (list[2] + list[5] + list[8]) &&
sum == (list[0] + list[4] + list[8]) && sum == (list[2] + list[4] + list[6]))
return true;
return false;
}
Doing permutations ourselves:
import copy
def SeekOrderings(ListSoFar,ItemsRemaining):
for d in ItemsRemaining:
NewList = copy.deepcopy(ListSoFar)
NewList += [d]
NewRemaining = copy.deepcopy(ItemsRemaining)
NewRemaining.remove(d)
if NewRemaining == []:
Process(NewList)
else:
SeekOrderings(NewList,NewRemaining)
def Process(d):
global ways
success = 1
for i in [0,1,2]:
if not((d[i]+d[i+3]+d[i+6]==15)
and (d[3*i]+d[3*i+1]+d[3*i+2]==15)):
success = 0
if not ((d[0]+d[4]+d[8]==15)
and (d[2]+d[4]+d[6]==15)):
success = 0
ways += success
ways = 0
SeekOrderings([],[1,2,3,4,5,6,7,8,9])
print (ways)
Problem Loading...
Note Loading...
Set Loading...
There are 8 different valid arrangements:
2 7 6 2 9 4 4 3 8 4 9 2 6 1 8 6 7 2 8 1 6 8 3 4 9 5 1 7 5 3 9 5 1 3 5 7 7 5 3 1 5 9 3 5 7 1 5 9 4 3 8 6 1 8 2 7 6 8 1 6 2 9 4 8 3 4 4 9 2 6 7 2
Python 3.3: