-
Notifications
You must be signed in to change notification settings - Fork 3
/
Prime-factorisation.cpp
73 lines (63 loc) · 2.2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
>> Prime Factorisation
>>> Resources:
T-1: https://cp-algorithms.com/algebra/factorization.html
T-2: https://www.geeksforgeeks.org/prime-factorization-using-sieve-olog-n-multiple-queries/
>>> Implementation:O(N log(log(N)))
int n;
vector<int> primefactor;
if(n%2==0)
{
primefactor.push_back(2);
while(n%2==0)
{
n/=2;
}
}
for(int j=3;j<=sqrt(n);j++)
{
if(n%j==0)
{
primefactor.push_back(j);
while(n%j==0)
{
n/=j;
}
}
}
if(n>2)
{
primefactor.push_back(n);
}
IMPLEMENTATION-2 : O(N log(N)) Useful in case of Integer Factorisation of N numbers.
const int N=10000000;
int prime[N];
vector<int> primefact;
void precompute()
{
for(int i=0;i<N;i++)
{
prime[i]=i;
}
for(int i=2;i<N;i+=2)
{
prime[i]=2;
}
for(int i=3;i<=sqrt(N);i++)
{
for(int j=i*i;j<N;j+=i)
{
if(prime[j]==j)
{
prime[j]=i;
}
}
}
}
void getprimefact(int n)
{
while(n!=1)
{
primefact.push_back(prime[n]);
n/=prime[n];
}
}