Ormiston pairs: Difference between revisions
Content added Content deleted
(→{{header|Go}}: Updated in line with latest Wren version.) |
(C++ solution rewritten using primesieve library) |
||
Line 116: | Line 116: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Primesieve}} |
|||
<syntaxhighlight lang="cpp">#include <algorithm> |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
⚫ | |||
#include <iomanip> |
#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include < |
#include <utility> |
||
⚫ | |||
std::vector<bool> prime_sieve(int limit) { |
|||
std::vector<bool> sieve(limit, true); |
|||
⚫ | |||
sieve[0] = false; |
|||
if (limit > 1) |
|||
sieve[1] = false; |
|||
for (int i = 4; i < limit; i += 2) |
|||
sieve[i] = false; |
|||
for (int p = 3, sq = 9; sq < limit; p += 2) { |
|||
⚫ | |||
for (int q = sq; q < limit; q += p << 1) |
|||
⚫ | |||
⚫ | |||
sq += (p + 1) << 2; |
|||
⚫ | |||
return sieve; |
|||
⚫ | |||
class digit_set { |
class digit_set { |
||
public: |
public: |
||
digit_set() {} |
|||
explicit digit_set(uint64_t n) { |
|||
for (; n > 0; n /= 10) |
for (; n > 0; n /= 10) |
||
++count_[n % 10]; |
++count_[n % 10]; |
||
Line 153: | Line 138: | ||
private: |
private: |
||
int count_[10] = {}; |
int count_[10] = {}; |
||
⚫ | |||
class ormiston_pair_generator { |
|||
public: |
|||
ormiston_pair_generator() { prime_ = pi_.next_prime(); } |
|||
std::pair<uint64_t, uint64_t> next_pair() { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
prime_ = pi_.next_prime(); |
|||
⚫ | |||
⚫ | |||
return std::make_pair(prime, prime_); |
|||
⚫ | |||
⚫ | |||
private: |
|||
primesieve::iterator pi_; |
|||
uint64_t prime_; |
|||
digit_set digits_; |
|||
}; |
}; |
||
int main() { |
int main() { |
||
ormiston_pair_generator generator; |
|||
const int limit = 100000000; |
|||
⚫ | |||
std::vector<bool> sieve = prime_sieve(limit); |
|||
int count = 0, count1 = 0, count2 = 0; |
|||
std::cout << "First 30 Ormiston pairs:\n"; |
std::cout << "First 30 Ormiston pairs:\n"; |
||
for ( |
for (; count < 30; ++count) { |
||
auto [p1, p2] = generator.next_pair(); |
|||
std::cout << '(' << std::setw(5) << p1 << ", " << std::setw(5) << p2 |
|||
continue; |
|||
<< ')' << ((count + 1) % 3 == 0 ? '\n' : ' '); |
|||
⚫ | |||
} |
|||
if (count1 == 0 && p2 > 1000000) |
|||
std::cout << '\n'; |
|||
⚫ | |||
for (uint64_t limit = 1000000; limit <= 1000000000; ++count) { |
|||
if (count2 == 0 && p2 > 10000000) |
|||
auto [p1, p2] = generator.next_pair(); |
|||
if (p1 > limit) { |
|||
⚫ | |||
⚫ | |||
<< '\n'; |
|||
limit *= 10; |
|||
} |
} |
||
p1 = p2; |
|||
} |
} |
||
std::cout << "\nNumber of Ormiston pairs < 1,000,000: " << count1 << '\n'; |
|||
⚫ | |||
std::cout << "Number of Ormiston pairs < 100,000,000: " << count << '\n'; |
|||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 194: | Line 194: | ||
(75913, 75931) (76213, 76231) (76579, 76597) |
(75913, 75931) (76213, 76231) (76579, 76597) |
||
Number of Ormiston pairs < |
Number of Ormiston pairs < 1000000: 382 |
||
Number of Ormiston pairs < |
Number of Ormiston pairs < 10000000: 3722 |
||
Number of Ormiston pairs < |
Number of Ormiston pairs < 100000000: 34901 |
||
Number of Ormiston pairs < 1000000000: 326926 |
|||
</pre> |
</pre> |
||