Check if Matrix sum is prime or not | Javascript 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
let N = 4 , M = 5 ; function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function takeSum ( a ) { let s = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) s += a [ i ] [ j ] ; return s ; } let a = [ [ 1 , 2 , 3 , 4 , 2 ] , [ 0 , 1 , 2 , 3 , 34 ] , [ 0 , 34 , 21 , 12 , 12 ] , [ 1 , 2 , 3 , 6 , 6 ] ] ; let sum = takeSum ( a ) ; if ( isPrime ( sum ) ) document . write ( " " ) ; else document . write ( " " ) ;