-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeFactors_demo.cpp
281 lines (261 loc) · 5.88 KB
/
PrimeFactors_demo.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Name: PrimeFactors
Copyright:
Author: TALE PRAFULLKUMAR P. 072074
Date: 15/03/10 14:14
Description: This program will be used to factorize a given number.
It will display all the distinct prime factors of the
given number. The concept of trees is being used.
*/
#include<iostream>
#include <cmath>
using namespace std;
// We will be needing LinkedList to store the values of
// primes obtained and something that can be used as the
// stack to handle trees
// Formation of LinkedList
struct node
{
int info;
node *next;
};
class LinkedList
{
private:
node *L;
public:
LinkedList()
{
L = NULL;
};
int isempty();
void add(int item);
void print();
int traverse_and_check(int k);
void push(int k);
int pop();
};
int LinkedList::isempty()
{
int f=0;
if (L==NULL)
{
f=1;
}
return f;
}
void LinkedList::add(int item)
{
node *p;
p=new node;
p->info = item;
if(L==NULL)
{
p->next=NULL;
L=p;
}
else
{
p->next=L;
L=p;
}
}
void LinkedList::print()
{
if (isempty()==1)
{
return;
}
node *p;
p=L;
while(p!=NULL)
{
cout<<p->info<<endl;
p=p->next;
};
}
int LinkedList::traverse_and_check(int k)
{
int p;
int k1=k;
node *temp;
temp = L;
while(temp !=NULL)
{
p=temp->info;
while(k1%p == 0)
{
k1=k1/p;
};
temp=temp->next;
}
return k1;
}
void LinkedList::push(int item)
{
node *p;
p=new node;
if(L==NULL)
{
p->info=item;
p->next=NULL;
L=p;
}
else
{
p->info=item;
p->next=L;
L=p;
}
}
int LinkedList::pop()
{
int n;
n=L->info;
L=L->next;
return n;
}
//End of formation of LinkedList
int LeftChild(int n)
//Here is the function LeftChild(n) which will return
//the left child of given number.
//The LeftChild of a given number is defined as largest integer
//which is a factor of a given number and which is less than
//its square root.
//The RightChild of a given number is defined as corresponding
//factor to the LeftChild.
//Ex. Let the given number be 59832.
//The square root of a given number is 249.60
//but 216 is the largest integer which divides 59832 and
//which is less than its square root.
//The corresponding RightChild is 59832/216 = 277.
{
int a;
float n1 =n;//converting to float so as to able to use sqrt
a = int (sqrt(n1));
while(n%a !=0)
{
a=a-1;
};
return a;
}
int SquareCheck(int n)
// Since the prime factors of n^2 and n are same, this
// function will convert any square to its root (if the
// root is integer).
// This is to eliminate the duplicate pushing in stock and
// hence to reduce the calculations
{
int j=0;//flag veriable
int a;
while (j==0)
{
float n1=n;
a = (int) sqrt(n1);
if(n==a*a)
{
n=a;
}
else
{
j=1;
};
}
return n;
}
int main()
{
int n;
cout<<"Enter the number "<<endl;
cout<<endl;
cin>>n;
LinkedList P;// To store the prime numbers
LinkedList S;//Will be used as a stack
int nl,nr;//nl LeftChild of a given number. lly nr.
int e;//flag veriable
int k=0;//will be used to keep tab on stack
while (k!=1)
{
n = SquareCheck(n);
cout<<endl;
cout<<"2.THe output of squarecheck "<<n<<endl;
cout<<endl;
system("pause");
nl=LeftChild(n);
cout<<endl;
cout<<"3.The left chld of "<<n<<" is "<<nl<<endl;
cout<<endl;
system("pause");
if (nl==1)
{
P.push(n);
cout<<endl;
cout<<"4.The Prime Array P is "<<endl;
cout<<endl;
P.print();
cout<<endl;
system ("pause");
e=S.isempty();
if(e==1)
{
break;
}
n=S.pop();
n=P.traverse_and_check(n);
cout<<endl;
cout<<"5.The value of n "<<n<<endl;
cout<<endl;
system("pause");
if (n==1)
{
while(n==1)
{
cout<<endl;
cout<<"1.This is bcos n =1"<<endl;
cout<<endl;
system ("pause");
n=S.pop();
n=P.traverse_and_check(n);
}
};
}
else
{
nr=n/nl;
cout<<endl;
cout<<"6.The right chld of "<<n<<" is "<<nr<<endl;
cout<<endl;
system("pause");
S.push(nr);
n = nl;
};
cout<<endl;
cout<<"7.THe stack is "<<endl;
cout<<endl;
S.print();
cout<<endl;
system("pause");
k=S.isempty();//checking whether the stack
//is empty or not
//This is a check for the last element
if (k==1)
{
nl = LeftChild(n);
if(nl!=1)
{
k=0;
}
else
{
P.push(n);
}
}
}
cout<<endl;
cout<<"8.The final ans is "<<endl;
cout<<endl;
P.print();
cout<<endl;
system ("pause");
return 0;
}