-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeFactors.cpp
234 lines (214 loc) · 4.18 KB
/
PrimeFactors.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
/*
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 concpets of trees is being used.
*/
#include<iostream>
#include <cmath>
using namespace std;
// We will be needing LinkedList to store the values of
// primes obtaioned and something that can be used as the
// stack to handel 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)
{
long long int p;
long long 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()
{
long long 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.
{
int a;
float n1 = n;
a = int (sqrt(n1));
while(n%a !=0)
{
a=a-1;
};
return a;
}
int SquareCheck(long long 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 interger)
{
int j=0;
long long 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()
{
long long int n;
cout<<"Enter the number "<<endl;
cin>>n;
LinkedList P;// To store the prime numbers
LinkedList S;//Will be used as a stack
//S.push(n);
long long int nl,nr;
int e;//flag veriable
int k=0;
while (k!=1)
{
if (n==1)
{
while(n==1)
{
n=S.pop();
n=P.traverse_and_check(n);
}
};
n = SquareCheck(n);
nl=LeftChild(n);
if (nl==1)
{
P.push(n);
e=S.isempty();
if(e==1)
{
break;
}
n=S.pop();
n=P.traverse_and_check(n);
}
else
{
nr=n/nl;
S.push(nr);
n = nl;
};
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<<"The prime factors are "<<endl;
P.print();
system ("pause");
return 0;
}