-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_Factorization.cpp
More file actions
38 lines (35 loc) · 958 Bytes
/
Prime_Factorization.cpp
File metadata and controls
38 lines (35 loc) · 958 Bytes
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
bitset<N+1> isPrime; // Using bitset for memory efficiency
vector<int> prime_count(N+1, 0); // To store the number of primes up to each index
vi prime;
void sieve() {
isPrime.set(); // Set all bits to true
isPrime[0] = false;
isPrime[1] = false;
prime.push_back(2);
// Only consider odd numbers
for (int i = 3; i * i <= N; i += 2) {
if (isPrime[i]) {
prime.push_back(i);
for (int j = i * i; j <= N; j += i) // Start from i*i, not 2*i
isPrime[j] = false;
}
}
for (int i = sqrt(N) + 1; i <= N; i++) { // Add primes > sqrt(N)
if (isPrime[i])
prime.push_back(i);
}
}
void factorization(ll n)
{
vii v;
for(ll i=0; prime[i]*prime[i]<=n; i++){
if(n%prime[i]==0){
while(n%prime[i]==0){
v.push_back(prime[i]);
n/=prime[i];
}
}
}
if(n!=1) v.push_back(n);
// cout(v);
}