Skip to content

Commit 4efa4d2

Browse files
committed
Prime Factorization using SPF
1 parent b392928 commit 4efa4d2

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

Algorithm/25 Prime Factorization.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11

2+
/**
3+
4+
Prime Factorization by finding all primes
5+
-----------------------------------------
6+
7+
There are roughly N/log(N) primes <= N
8+
9+
As we need only prime factors upto sqrt(N) to prime factorize ,
10+
Complexity of prime factorization will be O( (sqrt(N)/log(sqrt(N)) * log(N) )
11+
12+
**/
13+
214
/** Which of the favors of your Lord will you deny ? **/
315

416
#include<bits/stdc++.h>
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
/**
3+
4+
Prime Factorization by finding smallest prime factor
5+
----------------------------------------------------
6+
7+
Modify sieve to find SPF
8+
9+
Complexity : O(logN)
10+
11+
**/
12+
13+
/** Which of the favors of your Lord will you deny ? **/
14+
15+
#include<bits/stdc++.h>
16+
using namespace std;
17+
18+
#define LL long long
19+
#define PII pair<int,int>
20+
#define PLL pair<LL,LL>
21+
#define MP make_pair
22+
#define F first
23+
#define S second
24+
#define INF INT_MAX
25+
26+
#define ALL(x) (x).begin(), (x).end()
27+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
28+
#define READ freopen("alu.txt", "r", stdin)
29+
#define WRITE freopen("vorta.txt", "w", stdout)
30+
31+
#include <ext/pb_ds/assoc_container.hpp>
32+
#include <ext/pb_ds/tree_policy.hpp>
33+
using namespace __gnu_pbds;
34+
35+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
36+
37+
/**
38+
PBDS
39+
-------------------------------------------------
40+
1) insert(value)
41+
2) erase(value)
42+
3) order_of_key(value) // 0 based indexing
43+
4) *find_by_order(position) // 0 based indexing
44+
**/
45+
46+
template<class T1, class T2>
47+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
48+
template <class T>
49+
ostream &operator <<(ostream &os, vector<T>&v);
50+
template <class T>
51+
ostream &operator <<(ostream &os, set<T>&v);
52+
53+
inline void optimizeIO()
54+
{
55+
ios_base::sync_with_stdio(false);
56+
cin.tie(NULL);
57+
}
58+
59+
const int nmax = 2e5+7;
60+
const LL LINF = 1e17;
61+
62+
template <class T>
63+
string to_str(T x)
64+
{
65+
stringstream ss;
66+
ss<<x;
67+
return ss.str();
68+
}
69+
70+
//bool cmp(const PII &A,const PII &B)
71+
//{
72+
//
73+
//}
74+
75+
/** Bit Sieve **/
76+
77+
const int pnmax = 1e7+7;
78+
LL LIM;
79+
bitset<pnmax> bs; /// can sieve upto 1e8 in ~ 1 sec
80+
int spf[pnmax];
81+
82+
void bit_sieve(LL upperbound)
83+
{
84+
LIM = upperbound + 1;
85+
bs.set(); /// set all bits to 1
86+
bs[0] = bs[1] = 0;
87+
for (LL i = 2; i <= LIM; i++) /** If I don't want to know the primes , it is enough to loop upto sqrt(LIM) here **/
88+
if (bs[i])
89+
{
90+
for (LL j = i * i; j <= LIM; j += i)
91+
bs[j] = 0 , spf[j] = i;
92+
spf[i] = i;
93+
}
94+
}
95+
96+
/** Prime Factorization **/
97+
98+
vector<LL> primeFactors(LL N)
99+
{
100+
vector<LL>factors;
101+
102+
while(N>1)
103+
{
104+
LL sml = spf[N];
105+
106+
while(N%sml==0)
107+
{
108+
factors.push_back(sml);
109+
N/=sml;
110+
}
111+
}
112+
113+
return factors;
114+
}
115+
116+
int main()
117+
{
118+
optimizeIO();
119+
120+
bit_sieve(1e6);
121+
122+
while(1)
123+
{
124+
LL x;
125+
cin>>x;
126+
vector<LL>pf = primeFactors(x);
127+
cout<<pf<<endl;
128+
}
129+
130+
return 0;
131+
}
132+
133+
/**
134+
**/
135+
136+
template<class T1, class T2>
137+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
138+
{
139+
os<<"{"<<p.first<<", "<<p.second<<"} ";
140+
return os;
141+
}
142+
template <class T>
143+
ostream &operator <<(ostream &os, vector<T>&v)
144+
{
145+
os<<"[ ";
146+
for(int i=0; i<v.size(); i++)
147+
{
148+
os<<v[i]<<" " ;
149+
}
150+
os<<" ]";
151+
return os;
152+
}
153+
154+
template <class T>
155+
ostream &operator <<(ostream &os, set<T>&v)
156+
{
157+
os<<"[ ";
158+
for(T i:v)
159+
{
160+
os<<i<<" ";
161+
}
162+
os<<" ]";
163+
return os;
164+
}

Miscellaneous/01 Operator Overloading for Sorting (Part 3).cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
/**
3+
4+
Overloading > operator
5+
======================
6+
7+
Needed for Min Heap Priority Queue / whenever "greater" is used as it will use > operator to determine which is greater
8+
9+
**/
10+
211
/** Which of the favors of your Lord will you deny ? **/
312

413
#include<bits/stdc++.h>

0 commit comments

Comments
 (0)