To determine if a given ( n \times n ) matrix is a magic square, we need to check the following conditions:
- All rows must sum to the same value.
- All columns must sum to the same value.
- Both diagonals (main diagonal and anti-diagonal) must sum to the same value.
- The sums of all rows, columns, and diagonals must be equal.
Additionally, for completeness, we can also verify:
- That the matrix contains all distinct integers from ( 1 ) to ( n^2 ), which is a common property of traditional magic squares.
Here is a step-by-step algorithm to determine if a matrix is a magic square:
Algorithm
-
Input Validation:
- Ensure the matrix is ( n \times n ). If not, it cannot be a magic square.
-
Calculate the Magic Sum:
- Compute the sum of the first row. This will serve as the reference "magic sum" (( S )).
-
Check Row Sums:
- Iterate through each row and compute its sum. If any row sum is not equal to ( S ), the matrix is not a magic square.
-
Check Column Sums:
- Iterate through each column and compute its sum. If any column sum is not equal to ( S ), the matrix is not a magic square.
-
Check Diagonal Sums:
- Compute the sum of the main diagonal (top-left to bottom-right).
- Compute the sum of the anti-diagonal (top-right to bottom-left).
- If either diagonal sum is not equal to ( S ), the matrix is not a magic square.
-
Optional: Verify Distinct Elements:
- Check that all elements in the matrix are distinct integers between ( 1 ) and ( n^2 ). If this condition is violated, the matrix is not a traditional magic square.
-
Return Result:
- If all checks pass, the matrix is a magic square. Otherwise, it is not.
Implementation in Python
Below is the Python implementation of the above algorithm:
def is_magic_square(matrix):
n = len(matrix)
# Step 1: Input validation
if any(len(row) != n for row in matrix):
return False # Not an n x n matrix
# Step 2: Calculate the magic sum (sum of the first row)
magic_sum = sum(matrix[0])
# Step 3: Check row sums
for row in matrix:
if sum(row) != magic_sum:
return False
# Step 4: Check column sums
for col_idx in range(n):
col_sum = sum(matrix[row_idx][col_idx] for row_idx in range(n))
if col_sum != magic_sum:
return False
# Step 5: Check diagonal sums
main_diag_sum = sum(matrix[i][i] for i in range(n))
anti_diag_sum = sum(matrix[i][n - 1 - i] for i in range(n))
if main_diag_sum != magic_sum or anti_diag_sum != magic_sum:
return False
# Optional: Step 6: Verify distinct elements
all_elements = [matrix[i][j] for i in range(n) for j in range(n)]
if sorted(all_elements) != list(range(1, n * n + 1)):
return False
# Step 7: All checks passed
return True
Example Usage
# Example matrix
matrix = [
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
]
print(is_magic_square(matrix)) # Output: True
Explanation of the Example
For the given matrix:
[
\begin{bmatrix}
2 & 7 & 6 \
9 & 5 & 1 \
4 & 3 & 8
\end{bmatrix}
]
- Magic Sum: The sum of the first row is ( 2 + 7 + 6 = 15 ).
- Row Sums: All rows sum to ( 15 ).
- Column Sums: All columns sum to ( 15 ).
- Diagonal Sums: Both diagonals sum to ( 15 ).
- Distinct Elements: The elements are ( {1, 2, 3, 4, 5, 6, 7, 8, 9} ), which are distinct and cover the range ( 1 ) to ( 9 ).
Thus, the matrix is a magic square.
Complexity Analysis
-
Time Complexity:
- Checking row sums: ( O(n^2) )
- Checking column sums: ( O(n^2) )
- Checking diagonal sums: ( O(n) )
- Verifying distinct elements: ( O(n^2 \log n) ) (due to sorting)
- Overall: ( O(n^2 \log n) )
-
Space Complexity:
- Storing all elements for sorting: ( O(n^2) )
Final Answer
The function is_magic_square determines whether a given ( n \times n ) matrix is a magic square. For the example provided, the output is:
[
\boxed{\text{True}}
]