seminar04 simplified 03_factorization
This commit is contained in:
parent
5165d16ffa
commit
1d5f2cac48
1 changed files with 1 additions and 23 deletions
|
@ -5,8 +5,6 @@
|
|||
|
||||
using std::cout, std::endl, std::pair, std::vector;
|
||||
|
||||
int next_prime(int n);
|
||||
|
||||
vector<pair<int, int>> factorization(int n) {
|
||||
if (n == 1) {
|
||||
return vector<pair<int,int>>{{1, 1}};
|
||||
|
@ -23,31 +21,11 @@ vector<pair<int, int>> factorization(int n) {
|
|||
}
|
||||
if (c)
|
||||
result.push_back(pair{d, c});
|
||||
d = next_prime(d);
|
||||
d++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_prime(int n) {
|
||||
bool prime = true;
|
||||
for(int i = 2; i <= static_cast<int>(std::sqrt(n)); i++) {
|
||||
if (n % i == 0) {
|
||||
prime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return prime;
|
||||
}
|
||||
|
||||
int next_prime(int n) {
|
||||
++n;
|
||||
while (true) {
|
||||
if (is_prime(n))
|
||||
return n;
|
||||
++n;
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, pair<int,int> p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
|
|
Reference in a new issue