Pierpont primes: Difference between revisions
Content deleted Content added
Added C++ solution |
|||
Line 220:
250th Pierpont prime of the first kind: 62518864539857068333550694039553
250th Pierpont prime of the second kind: 4111131172000956525894875083702271</pre>
=={{header|C++}}==
{{libheader|GMP}}
<lang cpp>#include <cassert>
#include <iomanip>
#include <iostream>
#include <set>
#include <vector>
#include <gmpxx.h>
using big_int = mpz_class;
bool is_prime(const big_int& n)
{
return mpz_probab_prime_p(n.get_mpz_t(), 25);
}
class n_smooth_generator
{
public:
explicit n_smooth_generator(int n);
big_int next();
private:
std::vector<int> primes_;
std::set<big_int> numbers_;
};
n_smooth_generator::n_smooth_generator(int n)
{
for (int p = 2; p <= n; ++p)
{
if (is_prime(p))
primes_.push_back(p);
}
numbers_.insert(1);
}
big_int n_smooth_generator::next()
{
assert(!numbers_.empty());
big_int result = *numbers_.begin();
numbers_.erase(numbers_.begin());
for (auto prime : primes_)
numbers_.insert(result * prime);
return result;
}
void print_vector(const std::vector<big_int>& numbers)
{
for (size_t i = 0, n = numbers.size(); i < n; ++i)
{
std::cout << std::setw(9) << numbers[i];
if ((i + 1) % 10 == 0)
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
const int max = 250;
std::vector<big_int> first, second;
int count1 = 0;
int count2 = 0;
n_smooth_generator smooth_gen(3);
big_int p1, p2;
while (count1 < max || count2 < max)
{
big_int n = smooth_gen.next();
if (count1 < max && is_prime(n + 1))
{
p1 = n + 1;
if (first.size() < 50)
first.push_back(p1);
++count1;
}
if (count2 < max && is_prime(n - 1))
{
p2 = n - 1;
if (second.size() < 50)
second.push_back(p2);
++count2;
}
}
std::cout << "First 50 Pierpont primes of the first kind:\n";
print_vector(first);
std::cout << "First 50 Pierpont primes of the second kind:\n";
print_vector(second);
std::cout << "250th Pierpont prime of the first kind: " << p1 << '\n';
std::cout << "250th Pierpont prime of the second kind: " << p2 << '\n';
return 0;
}</lang>
{{out}}
<pre>
First 50 Pierpont primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
First 50 Pierpont primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
250th Pierpont prime of the first kind: 62518864539857068333550694039553
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
=={{header|D}}==
|