Check if diagonal elements of a Matrix are Prime or not
Last Updated :
01 Jul, 2022
Given a matrix M[][] of dimension N*N, the task is to check if all the elements on the principal and cross diagonals of the matrix are prime or not. If found to be true, then print “Yes”. Otherwise print “No”.
Examples:
Input: M[][] = {{1, 2, 3, 13}, {5, 3, 7, 8}, {1, 2, 3, 4}, {5, 6, 7, 7}}
Output: Yes
Explanation:
Elements on the main diagonal are {1, 5, 3, 7}, which are all primes.
Elements on the cross diagonal are {13, 7, 2, 5}, which are all primes.
Therefore, the matrix satisfies all the necessary conditions.
Input: M[][] = {{1, 2, 3}, {5, 3, 7}, {5, 6, 4}}
Output: No
Approach: The idea is to use Sieve of Eratosthenes to check if a number is prime or not. Follow the steps below to solve the problem:
- Precompute and store the prime numbers using Sieve of Eratosthenes.
- Iterate a loop using variable i over the range [0, N – 1] and perform the following operations:
- Check if M[i][i], i.e. an element on the principal diagonal, is a prime number or not.
- Check if M[i][N – 1 – i], i.e. an element on the cross diagonal, is a prime number or not.
- If any of the above two conditions are not satisfied, print “NO” and break.
- Otherwise, print “YES”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime[1000005];
void SieveOfEratosthenes( int N)
{
memset (prime, true , sizeof (prime));
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p;
i <= N; i += p)
prime[i] = false ;
}
}
}
void checkElementsOnDiagonal(
vector<vector< int > > M, int N)
{
int flag = 1;
SieveOfEratosthenes(1000000);
for ( int i = 0; i < N; i++) {
flag &= (prime[M[i][i]]
&& prime[M[i][N - 1 - i]]);
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" ;
}
int main()
{
vector<vector< int > > M = {
{ 1, 2, 3, 13 },
{ 5, 3, 7, 8 },
{ 1, 2, 3, 4 },
{ 5, 6, 7, 7 }
};
int N = M.size();
checkElementsOnDiagonal(M, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static boolean [] prime = new boolean [ 1000005 ];
static void SieveOfEratosthenes( int N)
{
Arrays.fill(prime, true );
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p;
i <= N; i += p)
prime[i] = false ;
}
}
}
static void checkElementsOnDiagonal( int [][] M, int N)
{
int flag = 1 ;
SieveOfEratosthenes( 1000000 );
for ( int i = 0 ; i < N; i++) {
boolean flg = ( boolean )(prime[M[i][i]]
&& prime[M[i][N - 1 - i]]);
int val = (flg) ? 1 : 0 ;
flag &= val;
}
if (flag != 0 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main (String[] args)
{
int [][] M = {
{ 1 , 2 , 3 , 13 },
{ 5 , 3 , 7 , 8 },
{ 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 7 }
};
int N = M.length;
checkElementsOnDiagonal(M, N);
}
}
|
Python3
prime = [ True ] * 1000005
def SieveOfEratosthenes(N):
prime[ 0 ] = False
prime[ 1 ] = False
for p in range ( 2 , N + 1 ):
if p * p > N:
break
if (prime[p] = = True ):
for i in range (p * p, N + 1 , p):
prime[i] = False
def checkElementsOnDiagonal(M, N):
flag = 1
SieveOfEratosthenes( 1000000 )
for i in range (N):
flag & = (prime[M[i][i]] and prime[M[i][N - 1 - i]])
if (flag):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
M = [[ 1 , 2 , 3 , 13 ],
[ 5 , 3 , 7 , 8 ],
[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 7 ]]
N = len (M)
checkElementsOnDiagonal(M, N)
|
C#
using System;
class GFG
{
static bool [] prime = new bool [1000005];
static void SieveOfEratosthenes( int N)
{
Array.Fill(prime, true );
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= N; i += p)
prime[i] = false ;
}
}
}
static void checkElementsOnDiagonal( int [, ] M, int N)
{
int flag = 1;
SieveOfEratosthenes(1000000);
for ( int i = 0; i < N; i++) {
bool flg = ( bool )(prime[M[i, i]]
&& prime[M[i, N - 1 - i]]);
int val = (flg) ? 1 : 0;
flag &= val;
}
if (flag != 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main( string [] args)
{
int [, ] M = { { 1, 2, 3, 13 },
{ 5, 3, 7, 8 },
{ 1, 2, 3, 4 },
{ 5, 6, 7, 7 } };
int N = M.GetLength(0);
checkElementsOnDiagonal(M, N);
}
}
|
Javascript
<script>
var prime = Array(1000005).fill( true );
function SieveOfEratosthenes(N)
{
prime[0] = false ;
prime[1] = false ;
for ( var p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( var i = p * p;
i <= N; i += p)
prime[i] = false ;
}
}
}
function checkElementsOnDiagonal( M, N)
{
var flag = 1;
SieveOfEratosthenes(1000000);
for ( var i = 0; i < N; i++) {
flag &= (prime[M[i][i]]
&& prime[M[i][N - 1 - i]]);
}
if (flag)
document.write( "Yes" );
else
document.write( "No" );
}
var M = [
[ 1, 2, 3, 13 ],
[ 5, 3, 7, 8 ],
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 7 ]
];
var N = M.length;
checkElementsOnDiagonal(M, N);
</script>
|
Time Complexity: O(N*log (log N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...