-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorization.cpp
More file actions
40 lines (38 loc) · 930 Bytes
/
factorization.cpp
File metadata and controls
40 lines (38 loc) · 930 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
#include "factorization.h"
vector<vector<int>> factorize(int x) {
set<int> primes = primesto(int(x/2));
vector<vector<int>> facts;
for (set<int>::iterator it = primes.begin(); it != primes.end(); it++) {
int counter = 0;
if (x % *it == 0) {
++counter;
x /= *it;
while (x % *it == 0)
{
++counter;
x /= *it;
}
vector<int> inner;
inner.push_back(*it);
inner.push_back(counter);
facts.push_back(inner);
}
}
return facts;
}
void print_factorize(int x, ostream& os) {
os << x << ": ";
if (isPrime(x)) {
os << "Prime number \n";
return;
}
auto fact = factorize(x);
for (auto p : fact) {
os << p[0];
if (p[1] != 1) {
os << "^" << p[1];
}
os << " ";
}
os << "\n";
}