-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime Factorization .cpp
More file actions
73 lines (54 loc) · 1.22 KB
/
Prime Factorization .cpp
File metadata and controls
73 lines (54 loc) · 1.22 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
//Just copy this and move on to the solution
const int N = 1e6;
bool sieve[N];
int lp[N];
int k;
void pre()
{
for(int i=0;i<N;i++)
{
sieve[i] = true;
}
for(int i=2;i<N;i++)
{
if(sieve[i])
{
lp[i] = i;
for(int j=i+i;j<N;j+=i)
{
sieve[j] = false;
if(lp[j]==0)
{
lp[j] = i;
}
}
}
}
}
map<int,int>fact(int n)
{
map<int,int>m;
while(n>1)
{
int a = lp[n];
m[a]++;
n/=lp[n];
}
if(n>1)
{
m[n]++;
}
return m;
}
int32_t main()
{
pre(); // Remember to declare this in the main function
int n;
cin>>n;
map<int,int>m;
m = fact(n);
for(auto x: m)
{
cout<<x.first<<" "<<x.second<<endl;
}
}