forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_Factorization.cpp
More file actions
75 lines (73 loc) · 1.33 KB
/
Prime_Factorization.cpp
File metadata and controls
75 lines (73 loc) · 1.33 KB
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
#include<iostream>
#include<vector>
using namespace std;
vector<int>get_factors(vector<int>&primes,int n)
{
vector<int>ans;
ans.clear();
for(int i=0;i*i<=n;i++)
{
if(n%primes[i]==0)
{
ans.push_back(primes[i]);
while(n%primes[i]==0)
{
n=n/primes[i];
}
}
}
if(n!=1)
{
ans.push_back(n);
}
return ans;
}
vector<int>get_prime(vector<int>&prime,int n)
{
for(int i=3;i<=1000;i+=2)
{
prime[i]=1;
}
//mark all the even number as non prime
for(int i=3;i*i<=1000;i+=2)
{
if(prime[i]==1)
{
for(int j=i*i;j<=1000;j+=i)
{
prime[j]=0;
}
}
}
prime[0]=prime[1]=0;
prime[2]=1;
vector<int>fectors;
for(int i=2;i<=1000;i++)
{
if(prime[i]==1)
{
fectors.push_back(i);
}
}
return fectors;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//first task to genrate prime fectors and store it
vector<int>p(1000,0);
//now we have all prime number till n
int n;
cin>>n;
//enter n (the number you want to get prime fector)
vector<int>primes=get_prime(p,n);
vector<int>ans=get_factors(primes,n);
cout<<"Prime Factors are :";
for(auto i:ans)
{
cout<<i<<" ";
}
cout<<endl;
return 0;
}