De Polignac numbers: Difference between revisions

Added C++ solution
(→‎{{header|Phix}}: added alternative)
(Added C++ solution)
Line 263:
The 10000th De Polignac number is 273421
Found 19075 De Polignac numbers up to 500000
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
bool is_prime(int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
 
bool is_depolignac_number(int n) {
for (int p = 1; p < n; p <<= 1) {
if (is_prime(n - p))
return false;
}
return true;
}
 
int main() {
std::cout.imbue(std::locale(""));
std::cout << "First 50 de Polignac numbers:\n";
for (int n = 1, count = 0; count < 10000; n += 2) {
if (is_depolignac_number(n)) {
++count;
if (count <= 50)
std::cout << std::setw(5) << n
<< (count % 10 == 0 ? '\n' : ' ');
else if (count == 1000)
std::cout << "\nOne thousandth: " << n << '\n';
else if (count == 10000)
std::cout << "\nTen thousandth: " << n << '\n';
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
First 50 de Polignac numbers:
1 127 149 251 331 337 373 509 599 701
757 809 877 905 907 959 977 997 1,019 1,087
1,199 1,207 1,211 1,243 1,259 1,271 1,477 1,529 1,541 1,549
1,589 1,597 1,619 1,649 1,657 1,719 1,759 1,777 1,783 1,807
1,829 1,859 1,867 1,927 1,969 1,973 1,985 2,171 2,203 2,213
 
One thousandth: 31,941
 
Ten thousandth: 273,421
</pre>
 
1,777

edits