Resources:
প্রাইম জেনারেশন সিভ ও প্রাইম ফ্যাক্টরাইজেশন
Integer factorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
const int maxn = 1000005; | |
int prime[maxn]; | |
vector<int> p, pFactor; | |
void seive() | |
{ | |
memset(prime, 0, sizeof(prime)); | |
prime[0] = prime[1] = 1; | |
for(int i = 4; i < maxn; i += 2) prime[i] = 1; | |
for(int i = 3; i*i < maxn; i += 2){ | |
if(prime[i] == 0){ | |
for(int j = i*i; j < maxn; j += i) | |
prime[j] = 1; | |
} | |
} | |
for(int i = 2; i < maxn; i++){ | |
if(prime[i] == 0) p.push_back(i); | |
} | |
} | |
void primeFactorization(int x) | |
{ | |
for(int i = 0; i < p.size(); i++){ | |
while(x % p[i] == 0){ | |
x /= p[i]; | |
pFactor.push_back(p[i]); | |
} | |
if(x == 1) break; | |
} | |
if(x != 1) pFactor.push_back(x); | |
} | |
int main() | |
{ | |
seive(); | |
int n; | |
cin >> n; | |
primeFactorization(n); | |
for(int i = 0; i < pFactor.size(); i++) | |
cout << pFactor[i] << " "; | |
return 0; | |
} |
No comments:
Post a Comment