-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeFactors.cpp
62 lines (57 loc) · 961 Bytes
/
PrimeFactors.cpp
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
#include <iostream>
using namespace std;
bool isPrime(int n){
bool res=true;
for(int i=2;i*i<=n;i++){
if(n%i==0){
res=false;
}
}
return res;
}
void basic(int n){
for(int i=2;i*i<=n;i++){
if(isPrime(i)){
int x=i;
while(n%x==0){
cout<<i<<" ";
x*=i;
}
}
}
}
void optimized(int n){
if(n<=1)
return;
while(n%2==0){
cout<<"2"<<" ";
n/=2;
}
while(n%3==0){
cout<<"3 ";
n/=3;
}
for(int i=5;i*i<=n;i+=6){
while(n%i==0){
cout<<i<<" ";
n/=i;
}
while(n%(i+2)==0){
cout<<(i+2)<<" ";
n/=(i+2);
}
}
if(n>3){
cout<<n<<" ";
}
}
int main()
{
//Write a program to find prime factorization
int n;
cin>>n;
optimized(n);
cout<<endl;
basic(n);
return 0;
}