Primes which contain only one odd digit: Difference between revisions

Content deleted Content added
Querfeld (talk | contribs)
→‎J: add
Ulrie (talk | contribs)
No edit summary
 
(5 intermediate revisions by 4 users not shown)
Line 2:
 
;Task
Show on this page those primes under   '''1,000'''   which when expressed in decimal containscontain only one odd digit.
 
 
;Stretch goal:
Show on this page only the &nbsp; <u>count</u> &nbsp; of those primes under &nbsp; '''1,000,000''' &nbsp; which when expressed in decimal containscontain only one odd digit.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
Line 290 ⟶ 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 390 ⟶ 443:
</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>
 
=={{header|F_Sharp|F#}}==
Line 452 ⟶ 544:
dim as uinteger m = int(log(n-1)/log(5))
return int((n-1-5^m)/5^j)
end function
 
function evendig(n as uinteger) as uinteger
'produces the integers with only even digits
Line 613 ⟶ 705:
 
=={{header|J}}==
<syntaxhighlight lang="j"> getOneOdds=. #~ (1 = (2 +/@:| "."0@":)"0
primesTo=. i.&.(p:inv)
 
Line 628 ⟶ 720:
'''Works with gojq, the Go implementation of jq'''
 
As noted in the [[#Julia|Julia entry]], if only one digit of a prime is odd, then that digit is in the ones place. The first solution presented here uses this observation to generate plausible candidates. The second solution is more brutish and slower but simpler.
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
Line 640 ⟶ 732:
label $out | stream | if cond then break $out else . end;
 
# Output: an unbounded stream
def primes_with_exactly_one_odd_digit:
# Output: a stream of candidate strings, in ascending numerical order
Line 756 ⟶ 848:
There are 2560 primes with only one odd digit in base 10 between 1 and 1,000,000.
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Labeled[Cases[
NestWhileList[NextPrime,
Line 905 ⟶ 998:
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
Line 952 ⟶ 1,044:
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>put display ^1000 .grep: { ($_ % 2) && .is-prime && (.comb[^(*-1)].all %% 2) }
 
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n" ) {
cache $list;
Line 1,040 ⟶ 1,132:
odd = 0
str = string(n)
for m = 1 to len(str)
if number(str[m])%2 = 1
odd++
Line 1,120 ⟶ 1,212:
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>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func primes_with_one_odd_digit(upto, base = 10) {
Line 1,181 ⟶ 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
var maxDigits = 3
Line 1,194 ⟶ 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")
 
limit = 1e9 - 1
primes = Int.primeSieve(limit)