Primes which contain only one odd digit: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 1,157:
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>
|