Prime numbers which contain 123: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
No edit summary |
||
Line 204: | Line 204: | ||
{{out}} |
{{out}} |
||
<pre>Same as FreeBASIC entry.</pre> |
<pre>Same as FreeBASIC entry.</pre> |
||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <iostream> |
|||
#include <string> |
|||
#include <vector> |
|||
#include <cmath> |
|||
bool isPrime( int number ) { |
|||
if ( number < 2 ) { |
|||
return false ; |
|||
} |
|||
int stop = std::sqrt( static_cast<double>( number ) ) ; |
|||
for ( int i = 2 ; i <= stop ; ++i ) |
|||
if ( number % i == 0 ) |
|||
return false ; |
|||
return true ; |
|||
} |
|||
bool condition( int n ) { |
|||
std::string numberstring { std::to_string( n ) } ; |
|||
return isPrime( n ) && numberstring.find( "123" ) != std::string::npos ; |
|||
} |
|||
int main( ) { |
|||
std::vector<int> wantedPrimes ; |
|||
for ( int i = 1 ; i < 100000 ; i++ ) { |
|||
if ( condition( i ) ) |
|||
wantedPrimes.push_back( i ) ; |
|||
} |
|||
int count = 0 ; |
|||
for ( int i : wantedPrimes ) { |
|||
std::cout << i << ' ' ; |
|||
count++ ; |
|||
if ( count % 10 == 0 ) { |
|||
std::cout << std::endl ; |
|||
} |
|||
} |
|||
count = wantedPrimes.size( ) ; |
|||
for ( int i = wantedPrimes.back( ) + 1 ; i < 1000000 ; i++ ) { |
|||
if ( condition ( i ) ) |
|||
count++ ; |
|||
} |
|||
std::cout << std::endl ; |
|||
std::cout << "There are " << count << " such numbers below 1000000!\n" ; |
|||
return 0 ; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1123 1231 1237 8123 11239 12301 12323 12329 12343 12347 |
|||
12373 12377 12379 12391 17123 20123 22123 28123 29123 31123 |
|||
31231 31237 34123 37123 40123 41231 41233 44123 47123 49123 |
|||
50123 51239 56123 59123 61231 64123 65123 70123 71233 71237 |
|||
76123 81233 81239 89123 91237 98123 |
|||
There are 451 such numbers below 1000000! |
|||
</pre> |
|||
=={{header|CLU}}== |
=={{header|CLU}}== |