Skip to content

Commit e429de0

Browse files
committed
Number Theory Part 2 Added
1 parent 4c6365e commit e429de0

10 files changed

+1756
-0
lines changed

Algorithm/25 Prime Factorization.cpp

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
#define LL long long
8+
#define PII pair<int,int>
9+
#define PLL pair<LL,LL>
10+
#define MP make_pair
11+
#define F first
12+
#define S second
13+
#define INF INT_MAX
14+
15+
#define ALL(x) (x).begin(), (x).end()
16+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
17+
#define READ freopen("alu.txt", "r", stdin)
18+
#define WRITE freopen("vorta.txt", "w", stdout)
19+
20+
#include <ext/pb_ds/assoc_container.hpp>
21+
#include <ext/pb_ds/tree_policy.hpp>
22+
using namespace __gnu_pbds;
23+
24+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
25+
26+
/**
27+
28+
PBDS
29+
-------------------------------------------------
30+
1) insert(value)
31+
2) erase(value)
32+
3) order_of_key(value) // 0 based indexing
33+
4) *find_by_order(position) // 0 based indexing
34+
35+
**/
36+
37+
template<class T1, class T2>
38+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
39+
template <class T>
40+
ostream &operator <<(ostream &os, vector<T>&v);
41+
template <class T>
42+
ostream &operator <<(ostream &os, set<T>&v);
43+
44+
inline void optimizeIO()
45+
{
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL);
48+
}
49+
50+
const int nmax = 2e5+7;
51+
const LL LINF = 1e17;
52+
53+
template <class T>
54+
string to_str(T x)
55+
{
56+
stringstream ss;
57+
ss<<x;
58+
return ss.str();
59+
}
60+
61+
//bool cmp(const PII &A,const PII &B)
62+
//{
63+
//
64+
//}
65+
66+
/** Bit Sieve **/
67+
68+
const int pnmax = 1e8+7;
69+
LL LIM;
70+
bitset<pnmax> bs; /// can sieve upto 1e8 in ~ 1 sec
71+
vector<LL> primes;
72+
73+
void bit_sieve(LL upperbound)
74+
{
75+
LIM = upperbound + 1;
76+
bs.set(); /// set all bits to 1
77+
bs[0] = bs[1] = 0;
78+
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 **/
79+
if (bs[i])
80+
{
81+
for (LL j = i * i; j <= LIM; j += i)
82+
bs[j] = 0;
83+
primes.push_back(i);
84+
}
85+
}
86+
87+
bool isPrime(LL N)
88+
{
89+
if (N <= LIM)
90+
return bs[N]; /// O(1) for small primes
91+
92+
/** note: only work for N <= (last prime in "primes" vector)^2 . So, if Sieve is done upto 10^6 , can know isPrime upto 10^12 **/
93+
for (LL x:primes)
94+
if (N % x == 0)
95+
return false;
96+
return true; /// it takes longer time if N is a large prime!
97+
}
98+
99+
100+
101+
/** Prime Factorization **/
102+
103+
vector<LL> primeFactors(LL N)
104+
{
105+
vector<LL>factors;
106+
LL PF_idx = 0, PF = primes[PF_idx];
107+
while (N != 1 && (PF * PF <= N)) /// stop at sqrt(N), but N can get smaller
108+
{
109+
while (N % PF == 0)
110+
{
111+
N /= PF; /// remove this PF
112+
factors.push_back(PF);
113+
}
114+
PF = primes[++PF_idx]; /// only consider primes!
115+
}
116+
if (N != 1)
117+
factors.push_back(N); /// special case if N is actually a prime
118+
119+
return factors;
120+
}
121+
122+
LL number_of_different_PF(LL N)
123+
{
124+
LL PF_idx = 0, PF = primes[PF_idx], ans = 0;
125+
while (N != 1 && (PF * PF <= N))
126+
{
127+
if (N % PF == 0)
128+
ans++; /// count this pf only once
129+
while (N % PF == 0)
130+
N /= PF;
131+
PF = primes[++PF_idx];
132+
}
133+
if (N != 1)
134+
ans++;
135+
return ans;
136+
}
137+
138+
int main()
139+
{
140+
optimizeIO();
141+
142+
bit_sieve(1e6);
143+
144+
while(1)
145+
{
146+
LL x;
147+
cin>>x;
148+
vector<LL>pf = primeFactors(x);
149+
cout<<pf<<endl;
150+
cout<<number_of_different_PF(x)<<endl;
151+
}
152+
153+
return 0;
154+
}
155+
156+
/**
157+
158+
**/
159+
160+
template<class T1, class T2>
161+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
162+
{
163+
os<<"{"<<p.first<<", "<<p.second<<"} ";
164+
return os;
165+
}
166+
template <class T>
167+
ostream &operator <<(ostream &os, vector<T>&v)
168+
{
169+
os<<"[ ";
170+
for(int i=0; i<v.size(); i++)
171+
{
172+
os<<v[i]<<" " ;
173+
}
174+
os<<" ]";
175+
return os;
176+
}
177+
178+
template <class T>
179+
ostream &operator <<(ostream &os, set<T>&v)
180+
{
181+
os<<"[ ";
182+
for(T i:v)
183+
{
184+
os<<i<<" ";
185+
}
186+
os<<" ]";
187+
return os;
188+
}
189+
190+

0 commit comments

Comments
 (0)