-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab7.cpp
More file actions
63 lines (52 loc) · 1.12 KB
/
Lab7.cpp
File metadata and controls
63 lines (52 loc) · 1.12 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
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <utility>
#include <sstream>
#define N 10000000
using namespace std;
string num2str(int i) {
string Result;
ostringstream convert(Result);
convert << i;
return convert.str();
}
string hashKey(int i) {
string word = num2str(i);
sort(word.begin(), word.end());
return word;
}
map<string, vector<int> > findPrimes() {
bitset<N> isPrime;
map<string, vector<int> > result;
isPrime.set();
for (size_t i = 2; i < sqrt(N); i++) {
if (isPrime[i]) {
for (size_t j = i; j * i < N; j++) {
isPrime[i * j] = 0;
}
}
}
for (size_t i = 1000000; i <= 9999999; i++) {
if (isPrime[i]) {
string key = hashKey(i);
result[key].push_back(i);
}
}
return result;
}
bool cmp(pair<string, vector<int> > p1, pair<string, vector<int> > p2) {
return p1.second.size() < p2.second.size();
}
int lab7_main(int argc, char** argv) {
auto m = findPrimes();
auto max = max_element(m.begin(), m.end(), cmp);
cout << max->second.size() << endl;
for (auto x : max->second) {
cout << x << " " << endl;
}
return 0;
}