Smarandache-Wellin primes: Difference between revisions

Added C++ solution
m (Sigh. fix the rest of my typos)
(Added C++ solution)
Line 162:
19th: index 1087 prime 208614364610327343341589284471
20th: index 1188 prime 229667386663354357356628334581
</pre>
 
=={{header|C++}}==
{{libheader|GMP}}
{{libheader|Primesieve}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <string>
 
#include <primesieve.hpp>
 
#include <gmpxx.h>
 
using big_int = mpz_class;
 
class sw_number_generator {
public:
sw_number_generator() { next(); }
void next();
const std::string& number() const { return number_; }
uint64_t prime() const { return prime_; }
int index() const { return index_; }
std::string derived_number() const;
 
private:
primesieve::iterator pi_;
std::string number_;
uint64_t prime_;
int index_ = 0;
};
 
void sw_number_generator::next() {
++index_;
prime_ = pi_.next_prime();
number_ += std::to_string(prime_);
}
 
std::string sw_number_generator::derived_number() const {
int count[10] = {};
for (char c : number_)
++count[c - '0'];
std::string str;
for (int i = 0; i < 10; ++i) {
if (!str.empty() || count[i] != 0)
str += std::to_string(count[i]);
}
return str;
}
 
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 25) != 0;
}
 
std::string abbreviate(const std::string& str, size_t n) {
size_t len = str.size();
if (len > n)
return str.substr(0, n / 2) + "..." + str.substr(len - n / 2);
return str;
}
 
void print_sw_primes() {
std::cout
<< "Known Smarandache-Wellin primes:\n\n"
<< " |Index|Digits|Last prime| Smarandache-Wellin prime\n"
<< "----------------------------------------------------------------------\n";
sw_number_generator swg;
for (int n = 1; n <= 8; swg.next()) {
std::string num = swg.number();
if (is_probably_prime(big_int(num))) {
std::cout << std::setw(2) << n << "|"
<< std::setw(5) << swg.index() << "|"
<< std::setw(6) << num.size() << "|"
<< std::setw(10) << swg.prime() << "|"
<< std::setw(43) << abbreviate(num, 40) << '\n';
++n;
}
}
}
 
void print_derived_sw_primes(int count) {
std::cout << "First " << count << " Derived Smarandache-Wellin primes:\n\n"
<< " |Index|Derived Smarandache-Wellin prime\n"
<< "-----------------------------------------\n";
sw_number_generator swg;
for (int n = 1; n <= count; swg.next()) {
std::string num = swg.derived_number();
if (is_probably_prime(big_int(num))) {
std::cout << std::setw(2) << n << "|"
<< std::setw(5) << swg.index() << "|"
<< std::setw(32) << num << '\n';
++n;
}
}
}
 
int main() {
print_sw_primes();
std::cout << '\n';
print_derived_sw_primes(20);
}</syntaxhighlight>
 
{{out}}
<pre>
Known Smarandache-Wellin primes:
 
|Index|Digits|Last prime| Smarandache-Wellin prime
----------------------------------------------------------------------
1| 1| 1| 2| 2
2| 2| 2| 3| 23
3| 4| 4| 7| 2357
4| 128| 355| 719|23571113171923293137...73677683691701709719
5| 174| 499| 1033|23571113171923293137...10131019102110311033
6| 342| 1171| 2297|23571113171923293137...22732281228722932297
7| 435| 1543| 3037|23571113171923293137...30013011301930233037
8| 1429| 5719| 11927|23571113171923293137...11903119091192311927
 
First 20 Derived Smarandache-Wellin primes:
 
|Index|Derived Smarandache-Wellin prime
-----------------------------------------
1| 32| 4194123321127
2| 72| 547233879626521
3| 73| 547233979727521
4| 134| 13672766322929571043
5| 225| 3916856106393739943689
6| 303| 462696313560586013558131
7| 309| 532727113760586013758133
8| 363| 6430314317473636515467149
9| 462| 8734722823685889120488197
10| 490| 9035923128899919621189209
11| 495| 9036023329699969621389211
12| 522| 9337023533410210710923191219
13| 538| 94374237357103109113243102223
14| 624| 117416265406198131121272110263
15| 721| 141459282456260193137317129313
16| 738| 144466284461264224139325131317
17| 790| 156483290479273277162351153339
18| 852| 164518312512286294233375158359
19| 1087| 208614364610327343341589284471
20| 1188| 229667386663354357356628334581
</pre>
 
Line 206 ⟶ 346:
index 1188: prime 229667386663354357356628334581
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
1,777

edits