Check if Matrix sum is prime or not - GeeksforGeeks

05 Apr, 2021
Given a matrix mat[][], the task is to check that the sum of the elements of the matrix is prime or not.Examples:  
Input: mat[][] = {{1, 2}, {2, 1}} Output: NO Explanation: Sum of Matrix = 1 + 2 + 2 + 1 = 6 Since 6 is not prime. Therefore, output is NOInput: mat[][] = {{1, 2}, {2, 2}} Output: YES Explanation: Sum of matrix = 1 + 2 + 2 + 2 = 7 Since 7 is prime. Therefore, output is YES  
 
Approach: The idea is to find the sum of the matrix using two nested loops and then finally check that the sum of the matrix is a prime or not. If yes then the output is YES otherwise the output is NO.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to check// if the sum of matrix// is prime or not #include <bits/stdc++.h>using namespace std; const int N = 4, M = 5; // Function to check// whether a number// is prime or notbool isPrime(int n){    // Corner case    if (n <= 1)        return false;     // Check from 2 to n-1    for (int i = 2; i <= sqrt(n); i++)        if (n % i == 0)            return false;     return true;} // Function for to find the sum// of the given matrixint takeSum(int a[N][M]){    int s = 0;    for (int i = 0; i < N; i++)        for (int j = 0; j < M; j++)            s += a[i][j];     return s;} // Driver Codeint main(){     int a[N][M] = { { 1, 2, 3, 4, 2 },                    { 0, 1, 2, 3, 34 },                    { 0, 34, 21, 12, 12 },                    { 1, 2, 3, 6, 6 } };    int sum = takeSum(a);     if (isPrime(sum))        cout << "YES" << endl;    else        cout << "NO" << endl;    return 0;}
// Java implementation to check if// the sum of matrix is prime or notclass GFG{ static int N = 4, M = 5; // Function to check whether// a number is prime or notstatic boolean isPrime(int n){         // Corner case    if (n <= 1)        return false;     // Check from 2 to n-1    for(int i = 2; i <= Math.sqrt(n); i++)       if (n % i == 0)           return false;     return true;} // Function for to find the sum// of the given matrixstatic int takeSum(int a[][]){    int s = 0;         for(int i = 0; i < N; i++)       for(int j = 0; j < M; j++)          s += a[i][j];     return s;} // Driver Codepublic static void main(String[] args){    int a[][] = { { 1, 2, 3, 4, 2 },                  { 0, 1, 2, 3, 34 },                  { 0, 34, 21, 12, 12 },                  { 1, 2, 3, 6, 6 } };                       int sum = takeSum(a);     if (isPrime(sum))        System.out.print("YES" + "\n");    else        System.out.print("NO" + "\n");}} // This code is contributed by PrinciRaj1992
# Python3 implementation to check if# the sum of matrix is prime or notimport math # Function to check whether a number# is prime or notdef isPrime(n):     # Corner case    if (n <= 1):        return False;     # Check from 2 to n-1    for i in range(2, (int)(math.sqrt(n)) + 1):        if (n % i == 0):            return False;     return True; # Function for to find the sum# of the given matrixdef takeSum(a):     s = 0    for i in range(0, 4):        for j in range(0, 5):            s += a[i][j]     return s; # Driver Codea = [ [ 1, 2, 3, 4, 2 ],      [ 0, 1, 2, 3, 34 ],      [ 0, 34, 21, 12, 12 ],      [ 1, 2, 3, 6, 6 ] ];sum = takeSum(a); if (isPrime(sum)):    print("YES")else:    print("NO")     # This code is contributed by grand_master
// C# implementation to check if// the sum of matrix is prime or notusing System;class GFG{ static int N = 4, M = 5; // Function to check whether// a number is prime or notstatic Boolean isPrime(int n){         // Corner case    if (n <= 1)        return false;     // Check from 2 to n-1    for(int i = 2; i <= Math.Sqrt(n); i++)    if (n % i == 0)        return false;     return true;} // Function for to find the sum// of the given matrixstatic int takeSum(int [][]a){    int s = 0;         for(int i = 0; i < N; i++)    for(int j = 0; j < M; j++)        s += a[i][j];     return s;} // Driver Codepublic static void Main(String[] args){    int [][]a = new int[][]                {                    new int[] { 1, 2, 3, 4, 2 },                    new int[] { 0, 1, 2, 3, 34 },                    new int[] { 0, 34, 21, 12, 12 },                    new int[] { 1, 2, 3, 6, 6 }                };                     int sum = takeSum(a);     if (isPrime(sum))        Console.Write("YES" + "\n");    else        Console.Write("NO" + "\n");}} // This code is contributed by shivanisinghss2110
<script>    // Javascript implementation to check    // if the sum of matrix    // is prime or not         let N = 4, M = 5;       // Function to check    // whether a number    // is prime or not    function isPrime(n)    {        // Corner case        if (n <= 1)            return false;         // Check from 2 to n-1        for (let i = 2; i <= Math.sqrt(n); i++)            if (n % i == 0)                return false;         return true;    }     // Function for to find the sum    // of the given matrix    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("YES");    else        document.write("NO");     </script>
YES
 
Time Complexity: O(N*M) 
grand_master
princiraj1992
shivanisinghss2110
divyesh072019
palindrome
Mathematical
Matrix
School Programming
Mathematical
Matrix
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Matrix Chain Multiplication | DP-8
Program to find largest element in an array
Print a given matrix in spiral form
Rat in a Maze | Backtracking-2
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)