-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime cells
97 lines (89 loc) · 2.36 KB
/
Prime cells
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int k;
int prime(int);
int main()
{
int count=0,i,j,sum;
int n;
cin>>n;
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j==0&&i==0)
{
sum=a[i+1][j]+a[i][j+1];
if(prime(sum))
count++;
}
else if((i==n-1)&&(j==0))
{
sum=a[i-1][j]+a[i][j+1];
if(prime(sum))
count++;
}
else if(i==0&&((j!=0)&&(j!=n-1)))
{
sum=a[i][j+1]+a[i][j-1]+a[i+1][j];
if(prime(sum))
count++;
}
else if(i==n-1&&((j!=0)&&(j!=n-1)))
{
sum=a[i][j+1]+a[i][j-1]+a[i-1][j];
if(prime(sum))
count++;
}
else if((j==n-1)&&i==0)
{
sum=a[i][j-1]+a[i+1][j];
if(prime(sum))
count++;
}
else if((i==n-1)&&(j==n-1))
{
sum=a[i][j-1]+a[i-1][j];
if(prime(sum))
count++;
}
else if (j==n-1&&(i!=0&&i!=n-1))
{
sum=a[i][j-1]+a[i-1][j]+a[i+1][j];
if(prime(sum))
count++;
}
else if((j==0)&&((i!=0)&&(i!=n-1)))
{
sum=a[i-1][j]+a[i+1][j]+a[i][j+1];
if(prime(sum))
count++;
}
else
{
sum=a[i+1][j]+a[i-1][j]+a[i][j+1]+a[i][j-1];
if(prime(sum))
count++;
}
}
}
cout<<count;
return 0;
}
int prime(int sum)
{
for(int k=2;k<=sqrt(sum);k++)
if(sum % k == 0)
return 0;
return 1;
}