Consider the magic matrix:
A = np.array([[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[ 4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])
The matrix A has 5 row sums (one for each row), 5 column sums (one for each column) and two diagonal sums. These 12 sums should all be exactly the same. Verify that they are the same by printing them and “seeing” that they are the same.
Source Code:
import numpy as np
A=np.array([[17,24,1,8,15],
[23,5,7,14,16],
[4,6,13,20,22],
[10,12,19,21,3],
[11,18,25,2,9]])
# printing sum of each individual rows
for i in range(0,5):
print("Row",(i+1),"sum: ",end="")
summation=0
for j in range(0,5):
summation=summation+A[j][i]
print(summation)
# printing sum of each individual columns
for i in range(0,5):
print("Column",(i+1),"sum: ",end="")
summation=0
for j in range(0,5):
summation=summation+A[j][i]
print(summation)
# printing sum of individual each diagonal
summation=0
for i in range(0,5):
summation=summation+A[i][i]
print("Diagonal 1 sum: ",summation)
summation=0
for i in range(0,5):
summation=summation+A[i][4-i]
print("Diagonal 2 sum: ",summation)
Sample input and output:
Get Answers For Free
Most questions answered within 1 hours.