Primes which contain only one odd digit: Difference between revisions

no edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
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}}==
Line 1,157 ⟶ 1,211:
 
Found 2560 single-odd-digit primes upto 1000000.
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn is_prime( number : u32 ) -> bool {
let limit : u32 = (number as f32).sqrt( ).floor( ) as u32 ;
(2..=limit).all( | i | number % i != 0 )
}
 
fn has_one_odd( mut number : u32 ) -> bool {
let mut digits : Vec<u32> = Vec::new( ) ;
while number != 0 {
digits.push( number % 10 ) ;
number /= 10 ;
}
digits.into_iter( ).filter( | &d | d % 2 == 1 ).count( ) == 1
}
 
fn main() {
let mut solution_primes : Vec<u32> = Vec::new( ) ;
(2..1000).filter( | &d | is_prime( d ) && has_one_odd( d )).for_each( | n | {
solution_primes.push( n ) ;
} ) ;
println!("Prime numbers under 1000 with only one odd digit:") ;
println!("{:?}" , solution_primes ) ;
println!("Number of primes under 1000000 with only one odd digit : {}" ,
(2..1000000).filter( | &d | is_prime( d ) && has_one_odd( d ) ).count( ) ) ;
</syntaxhighlight>
{{out}}
<pre>
[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]
Number of primes under 1000000 with only one odd digit : 2560
</pre>
 
262

edits