-
Notifications
You must be signed in to change notification settings - Fork 172
/
Prime_Factors.cpp
37 lines (36 loc) · 1002 Bytes
/
Prime_Factors.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
//Method to calculate the prime factors of a given number
//Time complexity is almost square root of n or we can say O(sqrt(n)logn).
//space complexity is almost constant.
//Note: Base of log function is 2.
#include<bits/stdc++.h>
using namespace std;
vector<int> primeFactors(int n){
vector<int>res;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0){
int powercnt=0;
//divide untill unless it is divisible.
while(n%i==0){
n/=i;
powercnt++;
}
while(powercnt--){
res.push_back(i);
}
}
}
// if still n is still remaining that means it is prime number and
//also we are traversing only to sqrt of n so we have to count it.
if(n>1) res.push_back(n);
return res;
}
int main(){
int n;
cin>>n;
vector<int>factors=primeFactors(n);
// the factors
for(int j=0;j<factors.size();j++){
cout<<factors[j]<<' ';
}
return 0;
}