Check if Matrix sum is prime or not | Python3 implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def takeSum ( a ) : NEW_LINE INDENT s = 0 NEW_LINE for i in range ( 0 , 4 ) : NEW_LINE INDENT for j in range ( 0 , 5 ) : NEW_LINE INDENT s += a [ i ] [ j ] NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT a = [ [ 1 , 2 , 3 , 4 , 2 ] , [ 0 , 1 , 2 , 3 , 34 ] , [ 0 , 34 , 21 , 12 , 12 ] , [ 1 , 2 , 3 , 6 , 6 ] ] ; NEW_LINE sum = takeSum ( a ) ; NEW_LINE if ( isPrime ( sum ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT