Primes which contain only one odd digit: Difference between revisions

no edit summary
No edit summary
No edit summary
 
Line 289:
Found 46,708 one-odd-digit primes < 10^8 (100,000,000)
Found 202,635 one-odd-digit primes < 10^9 (1,000,000,000)</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iterator>
 
bool isPrime( int n ) {
int limit = static_cast<int>( std::floor( std::sqrt( static_cast<double>( n ) ) ) ) ;
for ( int i = 2 ; i < limit + 1 ; i++ ) {
if ( n % i == 0 ) {
return false ;
}
}
return true ;
}
 
bool hasOneOdd( int n ) {
std::vector<int> digits ;
while ( n != 0 ) {
digits.push_back( n % 10 ) ;
n /= 10 ;
}
return std::count_if( digits.begin( ) , digits.end( ) , []( int i ) { return
i % 2 == 1 ; } ) == 1 ;
}
 
bool condition( int n ) {
return isPrime( n ) && hasOneOdd( n ) ;
}
 
int main( ) {
std::vector<int> primes ;
for ( int i = 2 ; i < 1000 ; i++ ) {
if ( condition( i ) )
primes.push_back( i ) ;
}
std::cout << "Primes under 1000 with only one odd digit :\n" ;
std::copy( primes.begin( ) , primes.end( ) , std::ostream_iterator<int>( std::cout ,
" " ) ) ;
for ( int i = 1000 ; i < 1000000 ; i++ ) {
if ( condition( i ) )
primes.push_back( i ) ;
}
std::cout << "\nThe number of primes under 1000000 with only one odd digit is " <<
primes.size( ) << " !\n " ;
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>Primes under 1000 with only one odd digit :
3 5 7 23 29 41 43 47 61 67 83 89 223 227 229 241 263 269 281 283 401 409 421 443 449 461 463 467 487 601 607 641 643 647 661 683 809 821 823 827 829 863 881 883 887
The number of primes under 1000000 with only one odd digit is 2560 !
</pre>
 
=={{header|Delphi}}==
262

edits