Week 5 : Python : Exercise Problem on Lists - Matrix Multiplication Problem

I tried solving this problem using for loop But i am Not getting the correct answers. Can you please help me to figure out my error?

=def square_matrix_power(A,n):

    B = [item[:] for item in A]

    C = [item[:] for item in A]

    for n in range(1,n):

        for p in range(2):

            for q in range (2):

                for k in range (2):

                    C[p][q] += (A[p][k]) * (B[k][q])

        B = [item[:] for item in C]

    return(C)

While trying to print square_matrix(A,2) I am getting the value of A^3.

1 Like

After your first iteration (n=1), B=AA
In the 2nd iteration, you are multiplying B with A. Since B is A
A, end result will be A^3