Primes which contain only one odd digit: Difference between revisions

Content deleted Content added
Querfeld (talk | contribs)
m cleanup
Ulrie (talk | contribs)
No edit summary
 
(4 intermediate revisions by 3 users 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 387 ⟶ 441:
Count < 1,000,000 = 2560
Elapsed Time: 171.630 ms.
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
fastfunc nextprim prim .
repeat
prim += 1
until isprim prim = 1
.
return prim
.
func digodd n .
while n > 0
r += if n mod 2 = 1
n = n div 10
.
return r
.
p = 2
repeat
if digodd p = 1
write p & " "
.
p = nextprim p
until p >= 1000
.
</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
</pre>
 
Line 1,117 ⟶ 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>
 
Line 1,180 ⟶ 1,305:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./seq" for Lst
 
var limit = 999
Line 1,193 ⟶ 1,316:
}
Fmt.print("Primes under $,d which contain only one odd digit:", limit + 1)
for (chunk in Lst.chunks(results, 9)) Fmt.printtprint("$,%(maxDigits)d", chunkresults, 9)
System.print("\nFound %(results.count) such primes.\n")