GFG App
Open App
Browser
Continue

# Check if Matrix sum is prime or not

Given a matrix mat[][], the task is to check whether 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 NO

Input: 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 whether the sum of the matrix is prime or not. If yes then the output is YES otherwise the output is NO.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to check` `// if the sum of matrix ` `// is prime or not`   `#include ` `using` `namespace` `std;`   `const` `int` `N = 4, M = 5;`   `// Function to check ` `// whether a number` `// is prime or not` `bool` `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 matrix` `int` `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 Code` `int` `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

 `// Java implementation to check if ` `// the sum of matrix is prime or not` `class` `GFG{`   `static` `int` `N = ``4``, M = ``5``;`   `// Function to check whether` `// a number is prime or not` `static` `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 matrix` `static` `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 Code` `public` `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

 `# Python3 implementation to check if ` `# the sum of matrix is prime or not` `import` `math`   `# Function to check whether a number` `# is prime or not` `def` `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 matrix` `def` `takeSum(a):`   `    ``s ``=` `0` `    ``for` `i ``in` `range``(``0``, ``4``):` `        ``for` `j ``in` `range``(``0``, ``5``):` `            ``s ``+``=` `a[i][j]`   `    ``return` `s;`   `# Driver Code` `a ``=` `[ [ ``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#

 `// C# implementation to check if ` `// the sum of matrix is prime or not` `using` `System;` `class` `GFG{`   `static` `int` `N = 4, M = 5;`   `// Function to check whether` `// a number is prime or not` `static` `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 matrix` `static` `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 Code` `public` `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`

## Javascript

 ``

Output:

`YES`

Time Complexity: O(N*M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up