-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeFactors_fast_demo.cpp
187 lines (176 loc) · 4.24 KB
/
PrimeFactors_fast_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
/*
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>
#include "LinkedList.h"
#include "GetDown.h"
using namespace std;
static LinkedList PF;// To store the prime factors
static GetDown GD;
int Check_BasicPrimes(int n)
//This function is to check whether the given number
//is divisible by basic primes i.e. {2,3,5,7,11,13}
//and to reduce the no if it is divisible.
{
int P_basic[6]={2,3,5,7,11,13};
for(int i=0;i<6;i++)
{
if(n%P_basic[i]==0)
{
PF.push(P_basic[i]);
}
while(n%P_basic[i]==0)
{
n=n/P_basic[i];
}
}
return n;
}
int LeftChild(int n)
//Here is the function LeftChild(n) which will return
//the left child of given number.
//Finding the LeftChild is fasten using class GetDown.h
{
int a;
float n1=n;
a = int (sqrt(n1));
cout<<"\n The first apprimation for square root "<<a<<endl;
//We can write a = 30030*q + r
int q = a/30030;
int r = a%30030;
int i=-1;
i = GD.Search(r);
a = GD.display(i);
cout<<"\nTo put the in the form 30030*q + P[i] "<<a<<endl;
while(n%a !=0)
{
a=GD.GoDown(a,i);
cout<<a<<endl;
i=i-1;
if(i==-1)
{
q=q-1;
i=3248;
}
};
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 interger)
{
int j=0;
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;
cin>>n;
LinkedList S;//Will be used as a stack
int nl,nr;//nl=leftchild ,nr=rightchild
int e;//flag veriable
int k=0;
n = Check_BasicPrimes(n);
cout<<"\nNumber after reduing it by basic primes "<<n<<endl;
cout<<endl;
system("pause");
if(n==1)
{
cout<<"\nPrime factors are "<<endl;
PF.print();
system("pause");
return 0;
}
while (k!=1)
{
if (n==1)
{
while(n==1)
{
n=S.pop();
n=PF.traverse_and_check(n);
}
};
cout<<"\nThe square check of "<<n;
n = SquareCheck(n);
cout<<" is "<<n<<endl;
cout<<endl;
system("pause");
cout<<"\nFor finding the left child of "<<n<<endl;
cout<<endl;
system("pause");
nl=LeftChild(n);
cout<<"\nThe left child of "<<n<<" is "<<nl<<endl;
cout<<"We checked only certain numbers not all"<<endl;
cout<<endl;
system("pause");
if (nl==1)
{
PF.push(n);
e=S.isempty();
if(e==1)
{
break;
}
n=S.pop();
n=PF.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);
cout<<"\n The left child of "<<n<<" is "<<nl<<endl;
cout<<"We checked only certain numbers not all"<<endl;
cout<<endl;
if(nl!=1)
{
k=0;
}
else
{
PF.push(n);
}
}
}
//This is to remove 1 if present
k=PF.pop();
if(k!=1)
{
PF.push(k);
};
cout<<"\nThe prime factors are "<<endl;
PF.print();
system ("pause");
return 0;
}