Anaprimes: Difference between revisions

m
C++ - simplified code
m (→‎{{header|Phix}}: changed <= to < so it now shows the first rather than last group found)
m (C++ - simplified code)
Line 35:
{{libheader|Primesieve}}
This takes about 70 seconds on my system. Memory usage is 4G.
<syntaxhighlight lang="cpp">#include <algorithmarray>
#include <iomanip>
#include <iostream>
#include <map>
Line 43 ⟶ 42:
#include <primesieve.hpp>
 
classusing digit_set {= std::array<int, 10>;
public:
digit_set() {}
explicit digit_set(uint64_t n) {
for (; n > 0; n /= 10)
++count_[n % 10];
}
bool operator==(const digit_set& other) const {
return std::equal(count_, count_ + 10, other.count_);
}
bool operator<(const digit_set& other) const {
return std::lexicographical_compare(other.count_, other.count_ + 10,
count_, count_ + 10);
}
 
digit_set get_digits(uint64_t n) {
private:
intdigit_set count_[10]result = {};
for (; n > 0; n /= 10)
};
++count_result[n % 10];
return result;
};
 
int main() {
std::cout.imbue(std::locale(""));
primesieve::iterator pi;
using map_type = std::map<digit_set, std::vector<uint64_t>>;
std::map<digit_set, std::vector<uint64_t>, std::greater<digit_set>>;
map_type anaprimes;
for (uint64_t limit = 1000; limit <= 10000000000;) {
Line 90 ⟶ 80:
limit *= 10;
}
anaprimes[digit_setget_digits(prime)].push_back(prime);
}
}</syntaxhighlight>
1,777

edits