Iccanobif primes: Difference between revisions
Content added Content deleted
(→J: fix wrong result) |
(Added C++ solution) |
||
Line 473: | Line 473: | ||
<pre> |
<pre> |
||
Same as Wren example. |
Same as Wren example. |
||
</pre> |
|||
=={{header|C++}}== |
|||
{{libheader|GMP}} |
|||
<syntaxhighlight lang="cpp">#include <algorithm> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
#include <string> |
|||
#include <gmpxx.h> |
|||
using big_int = mpz_class; |
|||
bool is_probably_prime(const big_int& n) { |
|||
return mpz_probab_prime_p(n.get_mpz_t(), 15) != 0; |
|||
} |
|||
big_int reverse(const big_int& n) { |
|||
auto str = n.get_str(); |
|||
std::reverse(str.begin(), str.end()); |
|||
return big_int(str, 10); |
|||
} |
|||
std::string to_string(const big_int& num, size_t max_digits) { |
|||
std::string str = num.get_str(); |
|||
size_t len = str.size(); |
|||
if (len > max_digits) { |
|||
str = str.substr(0, max_digits / 2) + "..." + |
|||
str.substr(len - max_digits / 2); |
|||
str += " ("; |
|||
str += std::to_string(len); |
|||
str += " digits)"; |
|||
} |
|||
return str; |
|||
} |
|||
int main() { |
|||
big_int f0 = 0, f1 = 1; |
|||
std::cout << "First 30 Iccanobif primes:\n"; |
|||
for (int count = 0; count < 30;) { |
|||
big_int f = f0 + f1; |
|||
auto p = reverse(f); |
|||
if (is_probably_prime(p)) { |
|||
++count; |
|||
std::cout << std::setw(2) << count << ": " << to_string(p, 40) |
|||
<< '\n'; |
|||
} |
|||
f0 = f1; |
|||
f1 = f; |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 30 Iccanobif primes: |
|||
1: 2 |
|||
2: 3 |
|||
3: 5 |
|||
4: 31 |
|||
5: 43 |
|||
6: 773 |
|||
7: 7951 |
|||
8: 64901 |
|||
9: 52057 |
|||
10: 393121 |
|||
11: 56577108676171 |
|||
12: 940647607443258103531 |
|||
13: 5237879497657222310489731409575442761 |
|||
14: 9026258083384996860449366072142307801963 |
|||
15: 19900335674812302969...34431012073266446403 (80 digits) |
|||
16: 77841137362967479985...52312097783685331923 (104 digits) |
|||
17: 37722585901567604188...29174997072830756131 (137 digits) |
|||
18: 75736193894876131595...50767238644714305761 (330 digits) |
|||
19: 17890336847332837620...13175300695235035913 (406 digits) |
|||
20: 92327163101729115305...27061468856047302507 (409 digits) |
|||
21: 50420157810698056253...67335124247362214481 (503 digits) |
|||
22: 30511012474739380092...69296158361330018201 (888 digits) |
|||
23: 46818547042693694555...08664543144645856321 (1020 digits) |
|||
24: 87101347853037819884...20128396998865227391 (1122 digits) |
|||
25: 17451656022543765336...20100243761843652461 (1911 digits) |
|||
26: 48989340566288399474...02930339234215909399 (1947 digits) |
|||
27: 12746927684958209654...53436989647994940101 (2283 digits) |
|||
28: 35746826582658751012...25010735912438195633 (3727 digits) |
|||
29: 87987175281297657706...48748727893681871587 (4270 digits) |
|||
30: 81807376367113798363...13687506007959668569 (10527 digits) |
|||
</pre> |
</pre> |
||