-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeStack.java
More file actions
163 lines (142 loc) · 2.12 KB
/
Copy pathPrimeStack.java
File metadata and controls
163 lines (142 loc) · 2.12 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package datastructures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PrimeStack
{
public static final String next = null;
listnode top;
int length;
class listnode
{
int data;
listnode next;
listnode(int data)
{
this.data=data;
this.next=next;
}
}
PrimeStack ()
{
this.top=null;
this.length=0;
}
public int length()
{
return length;
}
public boolean isEmpty()
{
return length==0;
}
public void push(int data)
{
listnode temp=new listnode(data);
temp.next=top;
top=temp;
length++;
}
public int pop()
{
try
{
if(isEmpty())
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println("stsck isEmpty");
}
int topdata = 0;
int result=topdata;
top=top.next;
length--;
return result;
}
public void display()
{
listnode n=top;
while(n!=null)
{
System.out.println(n.data);
n=n.next;
}
}
public int peek()
{
try
{
if(isEmpty())
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println("no data present");
}
return top.data;
}
public static List<Integer> isprimeAnagram(ArrayList<Integer> arr)
{
Integer array[] =arr.toArray(new Integer[arr.size()]);
ArrayList<Integer> bl=new ArrayList();
for(int j=0;j<array.length-1;j++)
{
for(int k=j+1;k<array.length-1;k++)
{
if(isPrime(array[j]+"",array[k]+""))
{
bl.add(array[j]);
bl.add(array[k]);
}
}
}
return bl;
}
public static boolean isPrime(String string,String string1)
{
char[]a=string.toCharArray();
char[] b=string1.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
String srr=new String(a);
String sr1=new String(b);
if(srr.equals(sr1));
{
return true;
}
}
public static void main(String[] args)
{
PrimeStack obj=new PrimeStack();
ArrayList<Integer> al=new ArrayList<Integer>();
List<Integer>al1=new ArrayList();
for(int i=1;i<=1000;i++)
{
int count=0;
for(int num=i;num>=1;num--)
{
if(i%num==0)
{
count=count+1;
}
}
if(count==2)
{
al.add(i);
}
}
System.out.println(al);
al1=isprimeAnagram(al);
System.out.println("the anagrams are");
for(Integer anagram:al1)
{
obj.push(anagram);
}
obj.display();
}
}