Week 5 - Topic Exercise Problem on Lists

Dear Team,

For the code,
def square_matrix_power(A,n):
output=A
temp=A
for i in range(n-1):

why have we done n-1 when we want the output to be to the power of n ?

Thanks & Regards,
Purvi Manghi

Hi @PURVI,
In the code snippet, we have given n-1 as the ending point, as indexing in python starts from 0
For instance, let’s say we want to have cube of the given matrix A, the loop will iterate from 0 to 3-1 which is 2.
Remember the fact that last value is not considered, so the iterator will take values 0 and 1 only.
If we multiply matrix A*A with A we’ll get