0% found this document useful (0 votes)
17 views2 pages

Challenge Problem Topic 2

The document contains a Python program that calculates the determinant of a square matrix using recursive Laplace expansion. It includes a main function to read the matrix size and its elements from user input, then computes and prints the determinant. An example input and output are provided, demonstrating the program's functionality.

Uploaded by

ducnc.2511233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Challenge Problem Topic 2

The document contains a Python program that calculates the determinant of a square matrix using recursive Laplace expansion. It includes a main function to read the matrix size and its elements from user input, then computes and prints the determinant. An example input and output are provided, demonstrating the program's functionality.

Uploaded by

ducnc.2511233
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

def determinant(matrix):

n = len(matrix)

# Base cases

if n == 1:

return matrix[0][0]

if n == 2:

return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]

# Recursive Laplace expansion

det = 0

for j in range(n):

# Build submatrix excluding row 0 and column j

sub = [[matrix[i][k] for k in range(n) if k != j] for i in range(1, n)]

det += ((-1) ** j) * matrix[0][j] * determinant(sub)

return det

def main():

# Read n

n = int(input().strip())

# Read matrix

matrix = []

for _ in range(n):

row = list(map(int, input().split()))

[Link](row)

# Compute determinant
print(determinant(matrix))

if __name__ == "__main__":

main()

-Input:

2134

0 -1 2 1

3205

-1 3 2 1

-Output:

35

You might also like