-
Notifications
You must be signed in to change notification settings - Fork 596
/
Prime Spiral Matrix.java
106 lines (93 loc) · 2.12 KB
/
Prime Spiral Matrix.java
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
98
99
100
101
102
103
104
105
106
import java.util.*;
class prsprl
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the matrix:-");
int m=sc.nextInt();
int a1[][]=new int[m][m];
System.out.println("Spiral Form:-");
int x=0,y=m-1;
int c=2,c1=1;
int p=0;
while(c1<=(m*m))
{
for(int t=x;t<=y;)
{
p=prime(c);
if(p==1)
{
a1[x][t]=c;
c++;
c1++;
t++;
}
else
c++;
}
for(int t=x+1;t<=y;)
{
p=prime(c);
if(p==1)
{
a1[t][y]=c;
c++;
c1++;
t++;
}
else
c++;
}
for(int t=y-1;t>=x+1;)
{
p=prime(c);
if(p==1)
{
a1[y][t]=c;
c++;
c1++;
t--;
}
else
c++;
}
for(int t=y;t>=x+1;)
{
p=prime(c);
if(p==1)
{
a1[t][x]=c;
c++;
c1++;
t--;
}
else
c++;
}
x++;
y--;
}
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a1[i][j]+"\t");
}
System.out.println();
}
}
int prime(int x)
{
int c=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;
}
if(c==2)
return 1;
else
return 0;
}
}