-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathprime_factorisation.cpp
52 lines (39 loc) · 1.35 KB
/
prime_factorisation.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
#include <bits/stdc++.h>
using namespace std;
// Wheel factorisation
vector<int> prime_factorise(int n){
vector<int> factors;
// This loop removes 2 as a factor n until 2 divides n
while(n%2 == 0){
n >>= 1;
factors.push_back(2);
}
// Here we divide n by each possible divisor d smaller than equal to floor(sq_root(n))
// as it is impossible for a number to have prime factor greater than floor(sq_root(n))
// The above loop helps in optimising the loop as we skip all even candidates, this it only checks 50% of the candidates.
for(int i=3 ; 1LL * i * i <= (long long)n ; i += 2){
while(n%i == 0){
n /= i;
factors.push_back(i);
}
}
// If we cannot find the divisor of n then it has to be a prime number so we check if n if greater than 1 for it to be a factor or not.
if(n > 1) factors.push_back(n);
return factors;
}
void print(int n, vector<int> &a){
// Utility function to print the result is proper format
cout<<"The factorisation of "<<n<<" is -\n";
for(int i : a) cout<<i<<' ';
cout<<'\n\n';
return;
}
int main(){
int example1 = 500;
int example2 = 45630;
vector<int> ans1 = prime_factorise(example1);
print(example1, ans1);
vector<int> ans2 = prime_factorise(example2);
print(example2, ans2);
return 0;
}