-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime-factorization.cpp
More file actions
49 lines (39 loc) · 842 Bytes
/
Copy pathprime-factorization.cpp
File metadata and controls
49 lines (39 loc) · 842 Bytes
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
#include <vector.h>
using namespace std;
// runs in O(loglog(n))
vector<int> getPrimes(int n) {
vector<int> primes;
while (n % 2 == 0) {
if(primes.size() == 0)
primes.push_back(2);
n /= 2;
}
for(int i = 3; i*i <= n; i += 2) {
int count = 1;
while (n % i == 0) {
if(count == 1)
primes.push_back(i);
n /= i;
count++;
}
}
if(n > 2)
primes.push_back(n);
return primes;
}
map<int, int> getPrimesAndOccurences(int n) {
map<int, int> primes;
while (n % 2 == 0) {
n /= 2;
primes[2]++;
}
for(int i = 3; i*i <= n; i += 2) {
while (n % i == 0) {
n /= i;
primes[i]++;
}
}
if(n > 2)
primes[n]++;
return primes;
}